Skip to content

Instantly share code, notes, and snippets.

@white-gecko
Last active March 1, 2021 17:37
Show Gist options
  • Save white-gecko/1a787e1911c479b3a13abc3e7103e7f7 to your computer and use it in GitHub Desktop.
Save white-gecko/1a787e1911c479b3a13abc3e7103e7f7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import timeit
REP = 100
def perf_set():
for i in range(REP):
'a' in {'a', 'b', 'c', 'd', 'e'}
'c' in {'a', 'b', 'c', 'd', 'e'}
'e' in {'a', 'b', 'c', 'd', 'e'}
def perf_set_large():
for i in range(REP):
'A' in {'A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
'9' in {'A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
'e' in {'A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
def perf_set_small():
for i in range(REP):
'A' in {'A', 'B'}
'B' in {'A', 'B'}
def perf_list():
for i in range(REP):
'a' in ['a', 'b', 'c', 'd', 'e']
'c' in ['a', 'b', 'c', 'd', 'e']
'e' in ['a', 'b', 'c', 'd', 'e']
def perf_list_large():
for i in range(REP):
'A' in ['A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
'9' in ['A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
'e' in ['A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
def perf_list_small():
for i in range(REP):
'A' in ['A', 'B']
'B' in ['A', 'B']
def perf_string():
for i in range(REP):
'a' in 'abcde'
'c' in 'abcde'
'e' in 'abcde'
def perf_string_large():
for i in range(REP):
'A' in 'ABCDEFabcdef0123456789'
'9' in 'ABCDEFabcdef0123456789'
'e' in 'ABCDEFabcdef0123456789'
def perf_string_small():
for i in range(REP):
'A' in 'AB'
'B' in 'AB'
# pre instantiated
def perf_set_prei():
struc = {'a', 'b', 'c', 'd', 'e'}
for i in range(REP):
'a' in struc
'c' in struc
'e' in struc
def perf_set_large_prei():
struc = {'A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
for i in range(REP):
'A' in struc
'9' in struc
'e' in struc
def perf_set_small_prei():
struc = {'A', 'B'}
for i in range(REP):
'A' in struc
'B' in struc
def perf_list_prei():
struc = ['a', 'b', 'c', 'd', 'e']
for i in range(REP):
'a' in struc
'c' in struc
'e' in struc
def perf_list_large_prei():
struc = ['A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
for i in range(REP):
'A' in struc
'9' in struc
'e' in struc
def perf_list_small_prei():
struc = ['A', 'B']
for i in range(REP):
'A' in struc
'B' in struc
def perf_string_prei():
struc = 'abcde'
for i in range(REP):
'a' in struc
'c' in struc
'e' in struc
def perf_string_large_prei():
struc = 'ABCDEFabcdef0123456789'
for i in range(REP):
'A' in struc
'9' in struc
'e' in struc
def perf_string_small_prei():
struc = 'AB'
for i in range(REP):
'A' in struc
'B' in struc
t_set = []
t_set_ = []
for i in range(0,100):
t_set.append(timeit.timeit("'a' in {'a', 'b', 'c', 'd', 'e'}"))
t_set.append(timeit.timeit("'c' in {'a', 'b', 'c', 'd', 'e'}"))
t_set.append(timeit.timeit("'e' in {'a', 'b', 'c', 'd', 'e'}"))
t_set_.append(timeit.timeit("'A' in {'A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}"))
t_set_.append(timeit.timeit("'9' in {'A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}"))
t_set_.append(timeit.timeit("'e' in {'A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}"))
t_list = []
t_list_ = []
for i in range(0,100):
t_list.append(timeit.timeit("'a' in ['a', 'b', 'c', 'd', 'e']"))
t_list.append(timeit.timeit("'c' in ['a', 'b', 'c', 'd', 'e']"))
t_list.append(timeit.timeit("'e' in ['a', 'b', 'c', 'd', 'e']"))
t_list_.append(timeit.timeit("'A' in ['A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"))
t_list_.append(timeit.timeit("'9' in ['A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"))
t_list_.append(timeit.timeit("'e' in ['A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"))
t_string = []
t_string_ = []
for i in range(0,100):
t_string.append(timeit.timeit("'a' in 'abcd'"))
t_string.append(timeit.timeit("'c' in 'abcde'"))
t_string.append(timeit.timeit("'e' in 'abcde'"))
t_string_.append(timeit.timeit("'A' in 'ABCDEFabcdef0123456789'"))
t_string_.append(timeit.timeit("'9' in 'ABCDEFabcdef0123456789'"))
t_string_.append(timeit.timeit("'e' in 'ABCDEFabcdef0123456789'"))
print("set:", sum(t_set)/len(t_set), "max:", max(t_set), "min:", min(t_set))
print("list:", sum(t_list)/len(t_list), "max:", max(t_list), "min:", min(t_list))
print("string:", sum(t_string)/len(t_string), "max:", max(t_string), "min:", min(t_string))
print("set:", sum(t_set_)/len(t_set_), "max:", max(t_set_), "min:", min(t_set_))
print("list:", sum(t_list_)/len(t_list_), "max:", max(t_list_), "min:", min(t_list_))
print("string:", sum(t_string_)/len(t_string_), "max:", max(t_string_), "min:", min(t_string_))
print("med")
print("set:", timeit.timeit(perf_set))
print("list:", timeit.timeit(perf_list))
print("string:", timeit.timeit(perf_string))
print("large")
print("set:", timeit.timeit(perf_set_large))
print("list:", timeit.timeit(perf_list_large))
print("string:", timeit.timeit(perf_string_large))
print("small")
print("set:", timeit.timeit(perf_set_small))
print("list:", timeit.timeit(perf_list_small))
print("string:", timeit.timeit(perf_string_small))
print("pre instantiated")
print("med")
print("set:", timeit.timeit(perf_set_prei))
print("list:", timeit.timeit(perf_list_prei))
print("string:", timeit.timeit(perf_string_prei))
print("large")
print("set:", timeit.timeit(perf_set_large_prei))
print("list:", timeit.timeit(perf_list_large_prei))
print("string:", timeit.timeit(perf_string_large_prei))
print("small")
print("set:", timeit.timeit(perf_set_small_prei))
print("list:", timeit.timeit(perf_list_small_prei))
print("string:", timeit.timeit(perf_string_small_prei))
@white-gecko
Copy link
Author

white-gecko commented Mar 1, 2021

did not know, how timeit works

we could also test, how the timing behaves in comparison for already instantiated sets/lists/string, as well as for small (two elements) and large ones.

somebody must have done that already. probably set wins slightly_smiling_face

@white-gecko
Copy link
Author

Now I have updated it and it appears that variable lookup is slower than set creation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment