Created
October 20, 2015 13:47
-
-
Save Makman2/37850615ade26c6a1000 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from timeit import * | |
def conditional_check(a, b): | |
if a is None or b is None: | |
return 10 | |
else: | |
return 24 | |
def tuple_check(a, b): | |
if None in (a, b): | |
return 10 | |
else: | |
return 24 | |
def set_check(a, b): | |
if None in {a, b}: | |
return 10 | |
else: | |
return 24 | |
def check(func): | |
for i in range(100): | |
func(i, i+1) | |
func(None, i) | |
func(i, None) | |
CHECKS = 10000 | |
print("Checking", CHECKS, "times") | |
print("Checking with expanded conditional expression:") | |
T = Timer(lambda: check(conditional_check)) | |
print(T.timeit(CHECKS)) | |
print("Checking when finding None inside a tuple:") | |
T = Timer(lambda: check(tuple_check)) | |
print(T.timeit(CHECKS)) | |
print("Checking when finding None inside a set:") | |
T = Timer(lambda: check(set_check)) | |
print(T.timeit(CHECKS)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment