Skip to content

Instantly share code, notes, and snippets.

@Makman2
Created October 20, 2015 13:47
Show Gist options
  • Save Makman2/37850615ade26c6a1000 to your computer and use it in GitHub Desktop.
Save Makman2/37850615ade26c6a1000 to your computer and use it in GitHub Desktop.
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