Why is this loop heavy code so slow in Python? Possible Project Euler spoilers

Paul Boddie paul at boddie.org.uk
Mon Sep 3 10:23:02 EDT 2007


On 3 Sep, 15:39, jwrweather... at gmail.com wrote:
> Thanks for all the answers to my question. I think what I need to take
> away from this is that xrange is an object

Indeed, but using xrange can be faster than converting your "for"
loops to "while" loops plus a counter; I converted your code to use
the latter arrangement and it was noticeably slower. Perhaps the
repeated invocation of each xrange object's next method is less
expensive than repeatedly obtaining new incremented int objects and
testing them against other int objects.

> - I thought it was just some loop construct, and that maths is slow in
> python - so avoid pathological looping.

You'd be wise to avoid doing unnecessary work deep within nested loops
in any programming language. Sadly, it's not possible for the compiler
to work out that some calculations can be lifted out of the innermost
loops in Python, since the various guarantees that make such
operations possible in other languages are not offered by Python.

> I remember the first time I tried Objective-C on OS X I used the
> NSNumber class for arithmetic - the results that time were pretty
> awful too!

Obviously, it'd be a fairer test if you used comparable numeric types
in both implementations, but a more capable numeric type would be
overkill for the C implementation of this program, and a less capable
numeric type doesn't exist for Python unless you're willing to use a
dialect such as Pyrex (as others have shown).

Paul




More information about the Python-list mailing list