Created
November 19, 2013 10:53
-
-
Save wasabi0522/7543658 to your computer and use it in GitHub Desktop.
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 | |
import math | |
import functools | |
def printspec(func): | |
@functools.wraps(func) | |
def wrapper(*args, **kwargs): | |
sttime = time.time() | |
# print func(*args, **kwargs) | |
func(*args, **kwargs) | |
entime = time.time() | |
return entime - sttime | |
return wrapper | |
@printspec | |
def prime_num_1(num): | |
a = range(num) | |
a[1] = 0 | |
for p in a: | |
if not p: | |
continue | |
elif p > int(math.sqrt(num)): | |
break | |
else: | |
for multi in range(p+p, num, p): | |
a[multi] = 0 | |
return [x for x in a if x] | |
@printspec | |
def prime_num_2(num): | |
b = range(2, num) | |
c = range(2, num / 2) | |
for i in range(0, int(math.sqrt(num))): | |
if b[i] != 0: | |
temp_num = b[i] | |
else: | |
continue | |
for j in c: | |
if ((j * temp_num - 2) < (num - 2)): | |
b[j * temp_num - 2] = 0 | |
else: | |
continue | |
return [i for i in b if i != 0] | |
@printspec | |
def prime_num_3(num): | |
a = [] | |
b = range(2, num) | |
for item in b: | |
for j in a: | |
if item % j == 0: | |
break | |
else: | |
a.append(item) | |
return a | |
if __name__ == "__main__": | |
funcs = [ prime_num_1, # perfect | |
prime_num_2, # remove remove | |
prime_num_3] # using 2 lists w/for else | |
num = 10000 | |
for func in funcs: | |
a = [] | |
for t in range(100): | |
a.append(func(num)) | |
print func.__name__, float(sum(a)/len(a)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment