Skip to content

Instantly share code, notes, and snippets.

@uptownnickbrown
Created October 12, 2018 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uptownnickbrown/fcb26b5632ffe81b64c76fd7c3381720 to your computer and use it in GitHub Desktop.
Save uptownnickbrown/fcb26b5632ffe81b64c76fd7c3381720 to your computer and use it in GitHub Desktop.
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