Speed up this code?
Frank Millman
frank at chagford.com
Sat May 27 03:23:40 EDT 2006
Gregory Petrosyan wrote:
> # Paul Rubin's version
> gregory at home:~$ python -mtimeit "import test2" "test2.primes(1000)"
> 100 loops, best of 3: 14.3 msec per loop
>
> # version from the Cookbook
> gregory at home:~$ python -mtimeit "import test1" "test1.primes(1000)"
> 1000 loops, best of 3: 528 usec per loop
You are quite right, Gregory, my timings are way off.
I have figured out my mistake. Paul's function is a generator. Unlike a
normal function, when you call a generator function, it does not
actually run the entire function, it simply returns a generator object.
It only runs when you iterate over it until it is exhausted. I was
effectively measuring how long it took to *create* the generator, not
iterate over it.
Thanks for correcting me.
Frank
More information about the Python-list
mailing list