[Tutor] Loop comparison

Steven D'Aprano steve at pearwood.info
Fri Apr 16 12:02:50 CEST 2010


On Fri, 16 Apr 2010 06:25:54 pm Stefan Behnel wrote:
> Alan Gauld, 16.04.2010 10:09:
> > Even the built in sum() will be faster than a while loop:
> >
> > result = sum(range(1000000000))
> >
> > although it still took 10 minutes on my PC.
>
> Did you mean to say "minutes" or rather "seconds" here? And did you
> really mean to use "range" or rather "xrange" (or "range" in Py3)?
>
>      sum(xrange(1000000000))
>
> clearly runs in 12 seconds for me on Py2.6, 

7 minutes for me in Python 2.5. The joys of low end hardware!

Are you sure you got the right number of zeroes?

> whereas the same with 
> "range" gives an error due to insufficient memory.

I'm not even going to try...




-- 
Steven D'Aprano


More information about the Tutor mailing list