Created
October 12, 2018 18:19
-
-
Save uptownnickbrown/fcb26b5632ffe81b64c76fd7c3381720 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 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