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 -- http://mail.python.org/mailman/listinfo/python-list