Created
July 5, 2014 18:30
-
-
Save muayyad-alsadi/92d9bf4a13f9efb0df3e to your computer and use it in GitHub Desktop.
primes optimizations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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