import time
def print_timing(func):
"""
a decorator function to time another function
time.clock() works on Windows systems only
you can use time.time() instead, but it gets updated less
frequently and can give 0.0 results on faster functions
"""
def inner(*arg):
"""*arg are the arguments of function func"""
t1 = time.clock()
res = func(*arg)
t2 = time.clock()
# time is in milliseconds
print('%s took %0.3f ms' % (func.__name__, (t2-t1)*1000.0))
return res
return inner
@print_timing
def isprime(x):
for i in range(2, x):
if x%i == 0:
return False
elif x%i != 0 and i == x-1:
return True
@print_timing
def isprime5(n):
if n == 2 or n == 3: return True
if n < 2 or n%2 == 0: return False
if n < 9: return True
if n%3 == 0: return False
sqr = int(n**0.5)
f = 5
while f <= sqr:
if n%f == 0: return False
if n%(f+2) == 0: return False
# loop every 6th integer
f += 6
return True
isprime(13999979) # isprime took 404.684 ms
isprime5(13999979) # isprime5 took 0.022 ms
isprime5(135999979) # isprime5 took 0.610 ms
isprime(135999979) # MemoryError

def isprime(x):
if x%2 != 0:
for i in range(3,x,2):
if x%i == 0:
return False
elif x%i != 0 and i == x-2:
return True
#A much faster way of processing the code#

def isprime(x):
if x%2 != 0:
for i in range(3,x,2):
if x%i == 0:
return False
elif x%i != 0 and i == x-2:
return True
elif x == 0 or x == 1 or x == 2:
return False
else:
return False

This is a program that ask input from the user about the characteristics of a gemstone then prints to the screen. Things like color, price and name. I have written ...