Created
August 11, 2015 02:05
-
-
Save csm10495/42d773742a3c035d0b2a 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
""" | |
iseq.py - A script for comparing the speed of is, ==, and neither with boolean values | |
csm10495 - Charles Machalow - MIT License | |
Hint: Usually == is slowest. Neither is fastest | |
""" | |
import timeit | |
def testN(n=1000): | |
""" | |
Description: | |
Runs a loop n times comparing a boolean value using neither "is" or "==" | |
""" | |
for i in range(n): | |
if True: | |
pass | |
else: | |
pass | |
def testIs(n=1000): | |
""" | |
Description: | |
Runs a loop n times comparing a boolean value using "is" | |
""" | |
for i in range(n): | |
if True is True: | |
pass | |
else: | |
pass | |
def testEq(n=1000): | |
""" | |
Description: | |
Runs a loop n times comparing a boolean value using "==" | |
""" | |
for i in range(n): | |
if True == True: | |
pass | |
else: | |
pass | |
def timeN(n=1000): | |
""" | |
Description: | |
Uses the timeit module to time n boolean comparisons using neither "==" or "is" | |
""" | |
return timeit.timeit("testN(n=" + str(n) + ")", "from __main__ import testN") | |
def timeIs(n=1000): | |
""" | |
Description: | |
Uses the timeit module to time n boolean comparisons using "is" | |
""" | |
return timeit.timeit("testIs(n=" + str(n) + ")", "from __main__ import testIs") | |
def timeEq(n=1000): | |
""" | |
Description: | |
Uses the timeit module to time n boolean comparisons using "==" | |
""" | |
return timeit.timeit("testEq(n=" + str(n) + ")", "from __main__ import testEq") | |
def isVsEqVsN(n=1000): | |
""" | |
Description: | |
Times n boolean comparisons using both "==", "is", and neither | |
Results are printed to stdout | |
""" | |
print("running %i trials of ==, is, and neither..." % n) | |
is_time = timeIs(n) | |
eq_time = timeEq(n) | |
n_time = timeN(n) | |
vals = {is_time: "is", eq_time: "eq", n_time: "neither"} | |
ordered_vals = [] | |
for i in sorted(vals): | |
ordered_vals.append(vals[i]) | |
print("is_time: %s seconds" % str(is_time)[:20]) | |
print("eq_time: %s seconds" % str(eq_time)[:20]) | |
print("n_time: %s seconds" % str(n_time)[:20]) | |
print("Order of speed (1st is fastest): %s" % ordered_vals) | |
def isVsEq(n=1000): | |
""" | |
Description: | |
Times n boolean comparisons using both "==" and "is" | |
Results are printed to stdout | |
""" | |
print("running %i trials of == and is..." % n) | |
is_time = timeIs(n) | |
eq_time = timeEq(n) | |
print("is_time: %s seconds" % str(is_time)[:20]) | |
print("eq_time: %s seconds" % str(eq_time)[:20]) | |
if is_time > eq_time: | |
print("eq is faster than is") | |
elif is_time == eq_time: | |
print("eq and is ran at the same speed... that is rather unlikely") | |
else: | |
print("is is faster than eq") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment