Speed-up for loops

Tim Wintle tim.wintle at teamrubber.com
Fri Sep 3 15:26:48 CEST 2010


On Fri, 2010-09-03 at 08:52 +0200, Ulrich Eckhardt wrote:
> Tim Wintle wrote:
> > [..] 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
> 
> There is one thing that strikes me here: The code claims that each iteration
> there is a lookup of the 'next' field in the iterator. I would expect that
> this is looked up once before the loop only.
> 
> Can you confirm that or am I misinterpreting your intention here?

As Stefan and Hrvoje have posted, there is a lookup - but in 2.4 and
above it's straight off the C structure and compiled efficiently.

(I've been looking at 2.3's source recently and had forgotten the
optimisation)

Tim






More information about the Python-list mailing list