-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontains.py
29 lines (25 loc) · 964 Bytes
/
contains.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import timeit
setup = """data_list = ['left', 'right', 'up', 'down']
data_tuple = ('left', 'right', 'up', 'down')
data_set = {'left', 'right', 'up', 'down'}
data_frozenset = frozenset(('left', 'right', 'up', 'down'))
"""
tests = {
"front_list": """'left' in data_list""",
"front_tuple": """'left' in data_tuple""",
"front_set": """'left' in data_set""",
"front_frozenset": """'left' in data_frozenset""",
"back_list": """'down' in data_list""",
"back_tuple": """'down' in data_tuple""",
"back_set": """'down' in data_set""",
"back_frozenset": """'down' in data_frozenset""",
}
results = {}
for test_name, test_stmt in tests.items():
times = timeit.repeat(
setup=setup, stmt=test_stmt, repeat=100, number=100000, globals={}
)
avg_time = sum(times) / len(times)
results[test_name] = avg_time
for test_name, result in sorted(results.items(), key=lambda x: x[1]):
print(f"{test_name:>18}: {result:03g}")