Speed-up for loops

Tim Wintle tim.wintle at teamrubber.com
Thu Sep 2 08:55:16 EDT 2010


On Thu, 2010-09-02 at 12:02 +0200, Michael Kreim wrote:
> Hi,
> 
> I was comparing the speed of a simple loop program between Matlab and 
> Python.

> Unfortunately my Python Code was much slower and I do not understand why.

The main reason is that, under the hood, cpython does something like
this (in psudo-code)

itterator = xrange(imax)
while 1:
  next_attribute = itterator.next
  try:
    i = next_attribute()
  except:
    break
  a = a + 10

where C (and I'm assuming matlab) does this:

while 1:
  i = i + 1
  if (i > imax):
    break
  a = a + 10

And the function call in the python is fairly expensive on it's own.
Plus it has to do all the standard interpreter stuff for memory
management and deciding when to give up the GIL etc.

> Are there any ways to speed up the for/xrange loop?

leaving it in python, no. (well, "range" is faster in 2.x, but once you
get some cache misses due to increased memory usage it's much slower)

avoiding iteration by using list comprehensions can help a lot though as
it avoids most of the function calls.

If you really need to optimise it then you can convert that module to
cython by adding a cdef, and then compile it:

cdef int i
for i in xrange(imax):
     a = a + 10
print a

or you can write it in C it'll run a lot faster.





More information about the Python-list mailing list