-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_dataclass.py
62 lines (46 loc) · 1.65 KB
/
test_dataclass.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""Demo of dataclass vs non-dataclass performance."""
import timeit
times = 200
dataclass_time = timeit.timeit("""
from dataclasses import dataclass
@dataclass(frozen=True, slots=True)
class Point:
x: float
y: float
z: float
p = Point(1, 2, 3)
""", number=times)
nondataclass_time = timeit.timeit("""
class Point:
__match_args__ = ('x', 'y', 'z')
__slots__ = ('x', 'y', 'z')
def __init__(self, x: float, y: float, z: float) -> None:
object.__setattr__(self, 'x', x)
object.__setattr__(self, 'y', y)
object.__setattr__(self, 'z', z)
def __repr__(self):
cls = type(self).__name__
return f'{cls}(x={self.x!r}, y={self.y!r}, z={self.z!r})'
def __eq__(self, other):
if not isinstance(other, Point):
return NotImplemented
return (self.x, self.y, self.z) == (other.x, other.y, other.z)
def __hash__(self):
return hash((self.x, self.y, self.z))
def __setattr__(self, name, value):
raise AttributeError(f"Can't set attribute {name!r}")
def __delattr__(self, name):
raise AttributeError(f"Can't delete attribute {name!r}")
def __getstate__(self):
return (self.x, self.y, self.z)
def __setstate__(self, state):
fields = ('x', 'y', 'z')
for field, value in zip(fields, state):
object.__setattr__(self, field, value)
p = Point(1, 2, 3)
""", number=times)
# https://www.pythonmorsels.com/p/2cv33/
# importing 200 regular classes: 1ms
# importing 200 dataclasses: 224ms
print(f"importing 200 regular classes: {nondataclass_time*1000:.0f}ms")
print(f"importing 200 dataclasses: {dataclass_time*1000:.0f}ms")