Remarkable results with psyco and sieve of Eratosthenes

Klaas mike.klaas at gmail.com
Thu Nov 30 22:14:55 CET 2006


Klaus Alexander Seistrup wrote:
> Pekka Karjalainen wrote:
>
> > You can omit the call to math.sqrt if you test this instead.
> >
> >     y*y > x
> >
> > in place of if y > maxfact: .
>
> Or use
>
> 	sqrt = lambda x: x ** .5

Test it:

$ python -m timeit -s "from math import sqrt" "sqrt(5.6)"
1000000 loops, best of 3: 0.445 usec per loop
$ python -m timeit -s "sqrt = lambda x: x**.5" "sqrt(5.6)"
1000000 loops, best of 3: 0.782 usec per loop

Note that this overhead is almost entirely in function calls; calling
an empty lambda is more expensive than a c-level sqrt:

$ python -m timeit -s "sqrt = lambda x: x" "sqrt(5.6)"
1000000 loops, best of 3: 0.601 usec per loop

Just math ops:
$ python -m timeit -s "x = 5.6" "x*x"
10000000 loops, best of 3: 0.215 usec per loop
$ python -m timeit -s "x = 5.6" "x**.5"
1000000 loops, best of 3: 0.438 usec per loop

Of course, who knows that psyco does with this under the hood.

-Mike




More information about the Python-list mailing list