Fortran vs Python - Newbie Question

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Mon Mar 26 19:30:58 EDT 2007


On Mon, 26 Mar 2007 15:53:56 -0700, sturlamolden wrote:

> Python is a very high-level language. That means there are certain
> things that put constraint on the attained speed. Most importantly:
> keep the number of interpreter evals as scarce as possible. If you
> make a for loop, the interpreter may evaluate the lines within the
> loop several times. Languages like C, C++ and Java really teach you
> bad habits when it comes to an interpreted language like Python. In
> these languages, loops are almost free. In Python, they may be very
> expensive as the interpreter is invoked multiple times.

Jeez-Louise, this is why my blood boils when I read ignora^H^H^H^H
misguided people referring to Python as "interpreted": it leads other
people to imagine that Python is interpreting lines over and over and over
again.

Is the interpreter invoked multiple times in a for loop? Let's find out.

Here is a function with a for loop:

def looper():
    x = 1
    for i in range(1000000):
        print x
        x += i
    return x


Let's look at the compiled code:

>>> dis.dis(looper)
  2           0 LOAD_CONST               1 (1)
              3 STORE_FAST               0 (x)

  3           6 SETUP_LOOP              35 (to 44)
              9 LOAD_GLOBAL              0 (range)
             12 LOAD_CONST               2 (1000000)
             15 CALL_FUNCTION            1
             18 GET_ITER
        >>   19 FOR_ITER                21 (to 43)
             22 STORE_FAST               1 (i)

  4          25 LOAD_FAST                0 (x)
             28 PRINT_ITEM
             29 PRINT_NEWLINE

  5          30 LOAD_FAST                0 (x)
             33 LOAD_FAST                1 (i)
             36 INPLACE_ADD
             37 STORE_FAST               0 (x)
             40 JUMP_ABSOLUTE           19
        >>   43 POP_BLOCK

  6     >>   44 LOAD_FAST                0 (x)
             47 RETURN_VALUE

Certainly Python isn't interpreting the lines in the for loop one million
times. It interprets them once, compiles them once, and executes them one
million times.


> But if you can
> 'vectorize' the loop statement into a one-liner (or a few lines) using
> slicing, list comprehensions or functional programming (e.g. the
> lambda, map and filter intrinsics), the overhead will be very small.

This is generally good advice because it moves the loop from
moderately fast Python code to very fast C code, not because Python is
interpreting each line over and over again.


-- 
Steven.




More information about the Python-list mailing list