Remarkable results with psyco and sieve of Eratosthenes

Steve Bergman steve at rueb.com
Wed Nov 29 16:54:49 EST 2006


Just wanted to report a delightful little surprise while experimenting
with psyco.
The program below performs astonoshingly well with psyco.

It finds all the prime numbers < 10,000,000

Processor is AMD64 4000+ running 32 bit.

Non psyco'd python version takes 94 seconds.

psyco'd version takes 9.6 seconds.

But here is the kicker.  The very same algorithm written up in C and
compiled with gcc -O3, takes 4.5 seconds.  Python is runng half as fast
as optimized C in this test!

Made my day, and I wanted to share my discovery.

BTW, can this code be made any more efficient?


============

#!/usr/bin/python -OO
import math
import sys
import psyco

psyco.full()

def primes():
    primes=[3]
    for x in xrange(5,10000000,2):
        maxfact = int(math.sqrt(x))
        flag=True
        for y in primes:
            if y > maxfact:
                break
            if x%y == 0:
                flag=False
                break
        if flag == True:
            primes.append(x)
primes()




More information about the Python-list mailing list