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

Gave me:

set: 0.015305645174812525 max: 0.03733481699600816 min: 0.013929764972999692
list: 0.03522656516249602 max: 0.06649509409908205 min: 0.013712542015127838
string: 0.019652776912165184 max: 0.021610349067486823 min: 0.018773655989207327
set: 0.015334985691588372 max: 0.02781434601638466 min: 0.013986732927151024
list: 0.1232732943216494 max: 0.2598540399922058 min: 0.013983548968099058
string: 0.021408032092731445 max: 0.02452279895078391 min: 0.02095441601704806
med
set: 4.33580927294679
list: 9.819138881983235
string: 5.124241805984639
large
set: 4.253970311023295
list: 38.27021916303784
string: 7.370228588930331
small
set: 3.2596762550529093
list: 4.000598898972385
string: 3.793448842014186
pre instantiated
med
set: 5.077883017016575
list: 10.996522731031291
string: 5.879405615036376
large
set: 5.410832240944728
list: 40.94489747402258
string: 8.189537095022388
small
set: 3.5728113469667733
list: 4.526656825910322
string: 4.068402747041546

@FlorianLudwig
Copy link

Here an alternative take which might have less variance

#!/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_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_string():
    for i in range(REP):
        'a' in 'abcd'
        'c' in 'abcde'
        'e' in 'abcde'


print("set:", timeit.timeit(perf_set))
print("list:", timeit.timeit(perf_list))
print("string:", timeit.timeit(perf_string))

result:

set: 6.141264303994831
list: 15.489817345980555
string: 7.29842415699386

@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