genexp performance problem?
Kent Johnson
kent at kentsjohnson.com
Tue May 30 22:41:31 EDT 2006
Delaney, Timothy (Tim) wrote:
> python -mtimeit "sum(int(L) for L in xrange(3000))"
> 100 loops, best of 3: 6.76 msec per loop
>
> python -mtimeit -s "g = (int(L) for L in xrange(3000))" "sum(g)"
> 1000000 loops, best of 3: 1.09 usec per loop
>
> The generator comprehension needs to create a new generator each time
> around.
Reusing the generator doesn't give a correct answer; after the first
sum() the generator is exhausted:
In [1]: g=(int(L) for L in xrange(10))
In [2]: sum(g)
Out[2]: 45
In [3]: sum(g)
Out[3]: 0
Kent
More information about the Python-list
mailing list