|
| 1 | +from collections import namedtuple |
| 2 | + |
| 3 | +STest = namedtuple("TEST", "a b c") |
| 4 | +a = STest(a=1, b=2, c=3) |
| 5 | + |
| 6 | + |
| 7 | +class Test(object): |
| 8 | + __slots__ = ["a", "b", "c"] |
| 9 | + |
| 10 | + def __init__(self) -> None: |
| 11 | + self.a = 1 |
| 12 | + self.b = 2 |
| 13 | + self.c = 3 |
| 14 | + |
| 15 | + |
| 16 | +b = Test() |
| 17 | + |
| 18 | +c = {"a": 1, "b": 2, "c": 3} |
| 19 | + |
| 20 | +d = (1, 2, 3) |
| 21 | +e = [1, 2, 3] |
| 22 | +f = (1, 2, 3) |
| 23 | +g = [1, 2, 3] |
| 24 | +key = 2 |
| 25 | + |
| 26 | +if __name__ == "__main__": |
| 27 | + from timeit import timeit |
| 28 | + |
| 29 | + print("Named tuple with a, b, c:") |
| 30 | + print(timeit("z = a.c", "from __main__ import a")) |
| 31 | + |
| 32 | + print("Named tuple, using index:") |
| 33 | + print(timeit("z = a[2]", "from __main__ import a")) |
| 34 | + |
| 35 | + print("Class using __slots__, with a, b, c:") |
| 36 | + print(timeit("z = b.c", "from __main__ import b")) |
| 37 | + |
| 38 | + print("Dictionary with keys a, b, c:") |
| 39 | + print(timeit("z = c['c']", "from __main__ import c")) |
| 40 | + |
| 41 | + print("Tuple with three values, using a constant key:") |
| 42 | + print(timeit("z = d[2]", "from __main__ import d")) |
| 43 | + |
| 44 | + print("List with three values, using a constant key:") |
| 45 | + print(timeit("z = e[2]", "from __main__ import e")) |
| 46 | + |
| 47 | + print("Tuple with three values, using a local key:") |
| 48 | + print(timeit("z = d[key]", "from __main__ import d, key")) |
| 49 | + |
| 50 | + print("List with three values, using a local key:") |
| 51 | + print(timeit("z = e[key]", "from __main__ import e, key")) |
0 commit comments