Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import sys
from timeit import timeit
TEST_CASES = [
0,
1,
-1,
2,
-2,
10,
-10,
3,
5,
8,
13,
21,
-3,
-5,
-8,
-13,
-21,
17,
19,
23,
29,
127,
128,
-127,
-128,
254,
255,
256,
384,
1234567890,
-254,
-255,
-256,
-384,
-1234567890,
987654321,
-987654321,
sys.maxsize,
-sys.maxsize -1]
def isEqualPrimitive(a, b):
return a == b
def isEqualSubtract(a, b):
return (a - b) == 0
def isEqualWillie(a, b):
try:
c = 1 / (a - b)
except:
return True
return False
def run(func):
for a in TEST_CASES:
for b in TEST_CASES:
func(a,b)
opsPerRun = len(TEST_CASES) ** 2
runs = 10000
total = timeit(lambda: run(isEqualPrimitive),number=runs)
print("Total time for",runs,"runs:",total,". Time per op:",total / (opsPerRun * runs))
total = timeit(lambda: run(isEqualSubtract),number=runs)
print("Total time for",runs,"runs:",total,". Time per op:",total / (opsPerRun * runs))
total = timeit(lambda: run(isEqualWillie),number=runs)
print("Total time for",runs,"runs:",total,". Time per op:",total / (opsPerRun * runs))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment