Skip to content

Instantly share code, notes, and snippets.

@muayyad-alsadi
Created July 5, 2014 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muayyad-alsadi/92d9bf4a13f9efb0df3e to your computer and use it in GitHub Desktop.
Save muayyad-alsadi/92d9bf4a13f9efb0df3e to your computer and use it in GitHub Desktop.
primes optimizations
import time
def get_primes(n):
primes=[]
compo=set()
in_compo=compo.__contains__
add_prime=primes.append
add_compo=compo.update
for i in range(2, n):
if in_compo(i): continue
add_prime(i)
add_compo(range(i*2, n,i))
def benchmark(f, n):
def wrapped(*a, **kw):
t0=time.time()
for i in range(n): f(*a, **kw)
print("dt=", time.time()-t0)
return wrapped
benchmark(get_primes, 500)(10000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment