Last active
August 29, 2015 14:04
-
-
Save mjpieters/f25eec1d085d56b50e5f to your computer and use it in GitHub Desktop.
Literal comparisons
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
$ bin/python test.py 4 1 | |
------------------------- | |
Configuration: | |
Number of elements to check against: 4 | |
Number of if statements: 1 | |
------------------------- | |
Concatenated boolean operators | |
0.1044412124203518 | |
List | |
0.06876948488876224 | |
Tuple | |
0.06889640966663137 | |
Set | |
0.03738487169379368 |
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
$ bin/python test.py 8 2 | |
------------------------- | |
Configuration: | |
Number of elements to check against: 8 | |
Number of if statements: 2 | |
------------------------- | |
Concatenated boolean operators | |
0.5024055746523663 | |
List | |
0.3394803279219195 | |
Tuple | |
0.3357834509317763 | |
Set | |
0.11226229946594685 |
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
$ bin/python test.py 1 1 -d | |
------------------------- | |
Setup: | |
import random | |
# Number to check against. It's randomly generated to fall over | |
# twice the range of the input values, so it'll hit the worst-case | |
# scenario (number not found) about half the time. | |
look_for = random.randint(0,1 * 2) | |
------------------------- | |
Boolean code: | |
# if look_for == 0 or look_for == 1 or ... : | |
if look_for == 0: | |
pass | |
------------------------- | |
List code: | |
if look_for in [ 0 ]: | |
pass | |
------------------------- | |
Tuple code: | |
if look_for in ( 0 , ): | |
pass | |
------------------------- | |
Set code: | |
if look_for in { 0 }: | |
pass | |
------------------------- | |
Configuration: | |
Number of elements to check against: 1 | |
Number of if statements: 1 | |
------------------------- | |
Concatenated boolean operators | |
0.03934748034225777 | |
List | |
0.03953318582149223 | |
Tuple | |
0.040348355600144714 | |
Set | |
0.03765739069320261 |
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
#! /usr/bin/env python3.4 | |
import random | |
import sys | |
import timeit | |
# Timeit configuration: Increasing repeat_num and number will improve | |
# the confidence in the measurement, but will take longer to evaluate. | |
# Number of times to repeat fresh setup before doing timeit runs | |
# The value we search for is generated once per setup, and then reused | |
# in the runs. | |
repeat_num=100 | |
# Number of timeit runs per setup | |
number=int(1e6) | |
# Code snippet configuration | |
# Number of elements to compare against test value | |
number_of_elements = int(sys.argv[1]) if (len(sys.argv) >= 2) else 4 | |
elements = range(number_of_elements) | |
# Repeated if statements (check a different value against elements each time) | |
number_of_ifs = int(sys.argv[2]) if (len(sys.argv) >= 3) else 1 | |
# Debug code snippets (prints them to std out) | |
debug = True if len(sys.argv) >= 4 else False | |
setup = """ | |
import random | |
# Number to check against. It's randomly generated to fall over | |
# twice the range of the input values, so it'll hit the worst-case | |
# scenario (number not found) about half the time. | |
look_for = random.randint(0,{number_of_elements} * 2) | |
""".format(number_of_elements=number_of_elements) | |
# Concatenated plain old boolean operators | |
boolean_or_str = ' or '.join([ | |
"look_for == {}".format(i) for i in elements]) | |
boolean_template = """ | |
# if var1 == look_for or var2 == look_for or ... : | |
if {boolean_ors}: | |
pass | |
""".format(boolean_ors=boolean_or_str) | |
# Sequences (list, tuple, set) | |
elements_str = ', '.join(map(str, elements)) | |
sequence_template = """ | |
if look_for in {{left}} {elements} {{right}}: | |
pass | |
""".format(elements=elements_str) | |
boolean_code = '\nlook_for += 1\n'. \ | |
join([boolean_template for i in range(number_of_ifs)]) | |
list_code = '\nlook_for += 1\n'.join([ | |
sequence_template.format(left='[', right=']') | |
for i in range(number_of_ifs)]) | |
tuple_code = '\nlook_for += 1\n'.join([ | |
sequence_template.format(left='(', right=', )') | |
for i in range(number_of_ifs)]) | |
set_code = '\nlook_for += 1\n'.join([ | |
sequence_template.format(left='{', right='}') | |
for i in range(number_of_ifs)]) | |
if debug: | |
print("-------------------------") | |
print("Setup:") | |
print(setup) | |
print("-------------------------") | |
print("Boolean code:") | |
print(boolean_code) | |
print("-------------------------") | |
print("List code:") | |
print(list_code) | |
print("-------------------------") | |
print("Tuple code:") | |
print(tuple_code) | |
print("-------------------------") | |
print("Set code:") | |
print(set_code) | |
# Now, we do a number of timeit runs, each with a slightly different setup | |
# (different number to look for different element variable values) | |
print("-------------------------") | |
print("Configuration:") | |
print("Number of elements to check against: ", number_of_elements) | |
print("Number of if statements: ", number_of_ifs) | |
print("-------------------------") | |
print("Concatenated boolean operators") | |
times = timeit.Timer(stmt=boolean_code, | |
setup=setup).repeat(repeat=repeat_num,number=number) | |
print(sum(times)/len(times)) | |
print("List") | |
times = timeit.Timer(stmt=list_code, | |
setup=setup).repeat(repeat=repeat_num,number=number) | |
print(sum(times)/len(times)) | |
print("Tuple") | |
times = timeit.Timer(stmt=tuple_code, | |
setup=setup).repeat(repeat=repeat_num,number=number) | |
print(sum(times)/len(times)) | |
print("Set") | |
times = timeit.Timer(stmt=set_code, | |
setup=setup).repeat(repeat=repeat_num,number=number) | |
print(sum(times)/len(times)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment