Skip to content

Instantly share code, notes, and snippets.

@sugar84
Created June 30, 2011 19:44
Show Gist options
  • Save sugar84/1057039 to your computer and use it in GitHub Desktop.
Save sugar84/1057039 to your computer and use it in GitHub Desktop.
python test measurement
#!/usr/bin/env python
def get_primes7(n):
if n < 2: return []
if n == 2: return [2]
# do only odd numbers starting at 3
s = []
for t in range(1, n/2):
s.append(t*2 + 1)
# n**0.5 simpler than math.sqr(n)
mroot = n ** 0.5
half = len(s)
i = 0
m = 3
while m <= mroot:
if s[i]:
j = (m*m-3)//2 # int div
s[j] = 0
while j < half:
s[j] = 0
j += m
i = i+1
m = 2*i+3
return [2]+[x for x in s if x]
for t in range(10):
res = get_primes7(10000000)
print "Found", len(res), "prime numbers."
"""
__END__
$ /usr/bin/time ./math_test.py
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
46.34user 0.38system 0:46.73elapsed 99%CPU
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment