slowdown on looping

David C. Fox davidcfox at post.harvard.edu
Sat Oct 4 02:31:10 EDT 2003


Trevor Perrin wrote:
> After running a simple loop around 100-200 million times, there's a
> speed drop of about a factor of 7.  This happens within several
> minutes (on a 1.7 Ghz machine), so it's not that hard to see.  I'm
> using Python 2.3.2 under Windows.
> 
> Is anyone familiar with this?
> 
> import time
> startTime = time.time()
> x = 0
> while 1:
>     x += 1
>     if x % 1000000 == 0:
>         endTime = time.time()
>         print "time = ", endTime - startTime, x
>         startTime = endTime            
>                
> 
> Trevor

I tried something similar to the loop above, but used a fixed loop size 
(for i in range(100000)) and varied the starting value of x.  I get a 
slowdown of about a factor of 5 if I start it with x > sys.maxint. 
Presumably that is because both += and % are now operating with long 
integers rather than integers.  Long integers have unlimited precision, 
which is not usually supported by hardware arithmetic, so it is bound to 
be slower.

Did you initially discover the problem with the loop above, or was that 
just a simplified test to analyze the effect?  If the latter, then 
another loop might slow down for an entierely different cause (e.g. 
memory use)

David





More information about the Python-list mailing list