Re: Remarkable results with psyco and sieve of Eratosthenes

2006-11-30 Thread Klaas
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)"

Re: Remarkable results with psyco and sieve of Eratosthenes

2006-11-30 Thread Klaus Alexander Seistrup
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 Cheers, -- Klaus Alexander Seistrup http://klaus.seistrup.dk/ -- http://mail.python.org/mailman/listinfo/python-li

Re: Remarkable results with psyco and sieve of Eratosthenes

2006-11-30 Thread Pekka Karjalainen
In article <[EMAIL PROTECTED]>, Steve Bergman wrote: >BTW, can this code be made any more efficient? >def primes(): >primes=[3] >for x in xrange(5,1000,2): >maxfact = int(math.sqrt(x)) >flag=True >for y in primes: >if y > maxfact: >br

Re: Remarkable results with psyco and sieve of Eratosthenes

2006-11-30 Thread bearophileHUGS
George Sakkis: > You can also save an attribute lookup for append; just add > append = primes.append > outside of the loop and replace primes.append(x) with append(x) > That should cut down a few fractions of second. We were talking about Psyco, and I think with Psyco (just released for Py 2.5, BT

Re: Remarkable results with psyco and sieve of Eratosthenes

2006-11-29 Thread George Sakkis
Will McGugan wrote: > > #!/usr/bin/python -OO > > import math > > import sys > > import psyco > > > > psyco.full() > > > > def primes(): > > primes=[3] > > for x in xrange(5,1000,2): > > maxfact = int(math.sqrt(x)) > > flag=True > > for y in primes: > >

Re: Remarkable results with psyco and sieve of Eratosthenes

2006-11-29 Thread Gabriel Genellina
At Wednesday 29/11/2006 20:35, Steve Bergman wrote: BTW, strictly speaking, shouldn't I be adding something to the floating point sqrt result, before converting to int, to allow for rounding error? If it is supposed to be 367 but comes in at 366., don't I potentially classify a composit

Re: Remarkable results with psyco and sieve of Eratosthenes

2006-11-29 Thread Steven D'Aprano
On Wed, 29 Nov 2006 15:35:39 -0800, Steve Bergman wrote: > BTW, strictly speaking, shouldn't I be adding something to the floating > point sqrt result, before converting to int, to allow for rounding > error? If you don't mind doing no more than one unnecessary test per candidate, you can just

Re: Remarkable results with psyco and sieve of Eratosthenes

2006-11-29 Thread dickinsm
On Nov 29, 6:59 pm, "Steve Bergman" <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > > BTW, can this code be made any more efficient? > > > I'm not sure, but the following code takes around 6 seconds on my > > 1.2Ghz iBook. How does it run on your machine? > > Hmm. Come to think of it,

Re: Remarkable results with psyco and sieve of Eratosthenes

2006-11-29 Thread Steve Bergman
[EMAIL PROTECTED] wrote: > > BTW, can this code be made any more efficient? > > I'm not sure, but the following code takes around 6 seconds on my > 1.2Ghz iBook. How does it run on your machine? > > Hmm. Come to think of it, my algorithm isn't the sieve. Anyway, this is indeed fast as long as y

Re: Remarkable results with psyco and sieve of Eratosthenes

2006-11-29 Thread Steve Bergman
Will McGugan wrote: > Some trivial optimizations. Give this a whirl. I retimed and got 9.7 average for 3 runs on my version. Yours got it down to 9.2. 5% improvement. Not bad. (Inserting '2' at the beginning doesn't seem to impact performance much.;-) ) BTW, strictly speaking, shouldn't I b

Re: Remarkable results with psyco and sieve of Eratosthenes

2006-11-29 Thread dickinsm
> BTW, can this code be made any more efficient? I'm not sure, but the following code takes around 6 seconds on my 1.2Ghz iBook. How does it run on your machine? def smallPrimes(n): """Given an integer n, compute a list of the primes < n""" if n <= 2: return [] sieve = range

Re: Remarkable results with psyco and sieve of Eratosthenes

2006-11-29 Thread Will McGugan
Beliavsky wrote: > > The number 1 is not generally considered to be a prime number -- see > http://mathworld.wolfram.com/PrimeNumber.html . > I stand corrected. -- blog: http://www.willmcgugan.com -- http://mail.python.org/mailman/listinfo/python-list

Re: Remarkable results with psyco and sieve of Eratosthenes

2006-11-29 Thread Beliavsky
Will McGugan wrote: > Steve Bergman wrote: > > 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 > > Actualy, it doesn't. You forgot 1 and 2. The n

Re: Remarkable results with psyco and sieve of Eratosthenes

2006-11-29 Thread Will McGugan
Steve Bergman wrote: > 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 Actualy, it doesn't. You forgot 1 and 2. Will McGugan -- blog: http://www.will

Re: Remarkable results with psyco and sieve of Eratosthenes

2006-11-29 Thread Will McGugan
> #!/usr/bin/python -OO > import math > import sys > import psyco > > psyco.full() > > def primes(): > primes=[3] > for x in xrange(5,1000,2): > maxfact = int(math.sqrt(x)) > flag=True > for y in primes: > if y > maxfact: > break >

Remarkable results with psyco and sieve of Eratosthenes

2006-11-29 Thread Steve Bergman
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.