Speed-up for loops

BartC bc at freeuk.com
Mon Sep 6 14:32:46 CEST 2010

"Stefan Behnel" <stefan_ml at behnel.de> wrote in message 
news:mailman.485.1283772019.29448.python-list at python.org...
> BartC, 06.09.2010 12:38:

>> (2) Integer arithmetic seems to go straight from 32-bits to long
>> integers; why not use 64-bits before needing long integers?
> You are making assumptions based on Python 2, I guess. Try Python 3.1 or 
> later instead, where the int and long types are unified. Also, the 
> implementation is a bit more complex than you appear to be thinking. Don't 
> forget that it has received serious benchmarking based optimisations.

That's true; wider arithmetic was less of an overhead in Python 3.

This shows the effect of making several small optimisations which might 
otherwise be dismissed: with the +100 test, the Python 3 faster wider 
arithmetic, *plus* the 4x loop unrolling, resulted in an 85% speed increase 
compared with Python 2 using the original loop. Which is pretty good 
considering Python 3 is generally slower than '2'.

>> This can be very fast, since
>> the loop counter need not be a Python object
> It still has to count, though.

That might be just a couple of machine instructions. Plus the bytecode 


More information about the Python-list mailing list