-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbench_class.py
90 lines (72 loc) · 2.41 KB
/
bench_class.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from collections import namedtuple
from dataclasses import dataclass
import typing
import sys
def attributes_in_class():
class Pet:
legs: int
noise: str
def __init__(self, legs, noise) -> None:
self.legs = legs
self.noise = noise
def __repr__(self):
return f"<Pet legs={self.legs} noise='{self.noise}'>"
for _ in range(100000):
dog = Pet(4, "woof")
str(dog)
def attributes_in_class_with_slots():
class Pet:
legs: int
noise: str
__slots__ = 'legs', 'noise'
def __init__(self, legs, noise) -> None:
self.legs = legs
self.noise = noise
def __repr__(self):
return f"<Pet legs={self.legs} noise='{self.noise}'>"
for _ in range(100000):
dog = Pet(4, "woof")
str(dog)
def attributes_in_dataclass():
@dataclass
class Pet:
legs: int
noise: str
for _ in range(100000):
dog = Pet(4, "woof")
str(dog)
if sys.version_info.minor >= 10:
def attributes_in_dataclass_with_slots():
@dataclass(slots=True)
class Pet:
legs: int
noise: str
for _ in range(100000):
dog = Pet(4, "woof")
str(dog)
def attributes_in_namedtuple():
Pet = namedtuple("Pet", "legs noise")
for _ in range(100000):
dog = Pet(4, "woof")
str(dog)
def attributes_in_namedtuple_type():
class Pet(typing.NamedTuple):
legs: int
noise: str
for _ in range(100000):
dog = Pet(4, "woof")
str(dog)
def attributes_in_dict():
for _ in range(100000):
dog = {"legs": 4, "noise": "woof"}
str(dog)
__benchmarks__ = [
(attributes_in_dataclass, attributes_in_class, "Class instead of dataclass"),
(attributes_in_dataclass, attributes_in_namedtuple, "Namedtuple instead of dataclass"),
(attributes_in_namedtuple, attributes_in_class, "class instead of namedtuple"),
(attributes_in_namedtuple, attributes_in_namedtuple_type, "namedtuple class instead of namedtuple"),
(attributes_in_class, attributes_in_dict, "dict instead of class"),
(attributes_in_class, attributes_in_class_with_slots, "class with slots")
]
if sys.version_info.minor >= 10:
__benchmarks__.append((attributes_in_dataclass, attributes_in_dataclass_with_slots, "dataclass with slots"))