-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathperformance.py
More file actions
51 lines (34 loc) · 1.25 KB
/
performance.py
File metadata and controls
51 lines (34 loc) · 1.25 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from collections import namedtuple
STest = namedtuple("TEST", "a b c")
a = STest(a=1, b=2, c=3)
class Test(object):
__slots__ = ["a", "b", "c"]
def __init__(self) -> None:
self.a = 1
self.b = 2
self.c = 3
b = Test()
c = {"a": 1, "b": 2, "c": 3}
d = (1, 2, 3)
e = [1, 2, 3]
f = (1, 2, 3)
g = [1, 2, 3]
key = 2
if __name__ == "__main__":
from timeit import timeit
print("Named tuple with a, b, c:")
print(timeit("z = a.c", "from __main__ import a"))
print("Named tuple, using index:")
print(timeit("z = a[2]", "from __main__ import a"))
print("Class using __slots__, with a, b, c:")
print(timeit("z = b.c", "from __main__ import b"))
print("Dictionary with keys a, b, c:")
print(timeit("z = c['c']", "from __main__ import c"))
print("Tuple with three values, using a constant key:")
print(timeit("z = d[2]", "from __main__ import d"))
print("List with three values, using a constant key:")
print(timeit("z = e[2]", "from __main__ import e"))
print("Tuple with three values, using a local key:")
print(timeit("z = d[key]", "from __main__ import d, key"))
print("List with three values, using a local key:")
print(timeit("z = e[key]", "from __main__ import e, key"))