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

Alex Martelli aleax at mac.com
Sun Sep 2 14:46:50 EDT 2007


Mark Dickinson <dickinsm at gmail.com> wrote:

> On Sep 2, 12:55 pm, al... at mac.com (Alex Martelli) wrote:
> > Mark Dickinson <dicki... at gmail.com> wrote:
> > > Well, for one thing, you're creating half a million xrange objects in
> > > the course of the search.  All the C code has
> > > to do is increment a few integers.
> >
> > I don't think the creation of xrange objects is a meaningful part of
> > Python's execution time here.  Consider:
> > [...]
> 
> Agreed---I just came to the same conclusion after doing some tests.
> So maybe it's the billion or so integer objects being created that
> dominate the running time?  (Not sure how many integer objects
> actually are created here: doesn't Python cache *some* small
> integers?)

Yep, some, say -5 to 100 or thereabouts; it also caches on a free-list
all the "empty" integer-objects it ever has (rather than returning the
memory for the system), so I don't think there's much optimization to be
had on that score either.


Alex



More information about the Python-list mailing list