[Tutor] Help Optimise Code

Kent Johnson kent37 at tds.net
Wed Nov 19 14:53:32 CET 2008


On Wed, Nov 19, 2008 at 8:13 AM, Richard Lovely
<roadierich at googlemail.com> wrote:
> I'm pretty new to code optimisation, so I thought I'd ask you all for advice.
>
> I'm making an iterative prime number generator.


You might be interested in this recipe and discussion:
http://code.activestate.com/recipes/366178/

According to Wikipedia, the siev of Atkin is faster than sieve of Eratosthenes:
http://en.wikipedia.org/wiki/Sieve_of_Atkin

> This is what I've got so far:

>            test = (not x % p) and -1 or p > sqrtX
>            if test == -1: # (not x % p) == true
>                break
>            elif test: # (p > sqrtX) == true
>                yield x
>                knownPrimes.append(x)
>                break

You are duplicating your tests, why not
if (not x % p):
  break
elif p > sqrtX:
  ...
?

Kent


More information about the Tutor mailing list