[Tutor] Loop comparison

Stefan Behnel stefan_ml at behnel.de
Fri Apr 16 10:25:54 CEST 2010


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, whereas the same with "range" 
gives an error due to insufficient memory.

Stefan



More information about the Tutor mailing list