Skip to content

Instantly share code, notes, and snippets.

@blackjack4494
Created July 21, 2021 22:10
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 blackjack4494/729505fafb0ae8d6e0980adb3762abdd to your computer and use it in GitHub Desktop.
Save blackjack4494/729505fafb0ae8d6e0980adb3762abdd to your computer and use it in GitHub Desktop.
Python Condition Benchmark
import timeit
a = True
b = True
c = False
d = True
e = False
true = True
false = False
# which is faster
def case1():
if a & b & c & d & e:
return
def case1b():
if a and b and c and d and e:
return
def case2():
if a:
if b:
if c:
if d:
if e:
return
def case3():
if false & true & true & true & true:
return
def case3b():
if false and true and true and true and true:
return
def case4():
if false:
if true:
if true:
if true:
if true:
return
def case5():
if True & True & True & True & True & True & True & False & True & True:
return
def case5b():
if True and True and True and True and True and True and True and False and True and True:
return
def case6():
if True:
if True:
if True:
if True:
if True:
if True:
if True:
if False:
if True:
if True:
return
print(timeit.timeit('case1()', globals=globals(), number=10000000))
print(timeit.timeit('case1b()', globals=globals(), number=10000000))
print(timeit.timeit('case2()', globals=globals(), number=10000000))
print(timeit.timeit('case3()', globals=globals(), number=10000000))
print(timeit.timeit('case3b()', globals=globals(), number=10000000))
print(timeit.timeit('case4()', globals=globals(), number=10000000))
print(timeit.timeit('case5()', globals=globals(), number=10000000))
print(timeit.timeit('case5b()', globals=globals(), number=10000000))
print(timeit.timeit('case6()', globals=globals(), number=10000000))
# OUTPUT
'''
0.9830773999999999
0.6398060999999999
0.6747931
0.9954288999999998
0.5990309000000003
0.5202876999999999
0.4735706000000004
0.49448640000000044
0.4494093999999995
'''
import timeit
a = True
b = True
c = False
d = True
e = False
true = True
false = False
# which is faster
def case1():
if a & b & c & d & e:
return
def case2():
if a:
if b:
if c:
if d:
if e:
return
def case3():
if false & true & true & true & true:
return
def case4():
if false:
if true:
if true:
if true:
if true:
return
def case5():
if True & True & True & True & True & True & True & False & True & True:
return
def case6():
if True:
if True:
if True:
if True:
if True:
if True:
if True:
if False:
if True:
if True:
return
print(timeit.timeit('case1()', globals=globals(), number=10000000))
print(timeit.timeit('case2()', globals=globals(), number=10000000))
print(timeit.timeit('case3()', globals=globals(), number=10000000))
print(timeit.timeit('case4()', globals=globals(), number=10000000))
print(timeit.timeit('case5()', globals=globals(), number=10000000))
print(timeit.timeit('case6()', globals=globals(), number=10000000))
# OUTPUT
'''
0.9239407000000001
0.6420651999999999
0.9261582000000002
0.5332373000000001
0.4501647000000002
0.4485041999999999
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment