forked from biqqles/dataclassy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmarks.py
78 lines (52 loc) · 1.48 KB
/
benchmarks.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
"""
Benchmarks for dataclassy.
Example output:
=== Simple class ===
dataclasses: 0.22464673200738616 seconds
dataclassy: 0.2270237009797711 seconds
dataclassy (slots): 0.1814715740038082 seconds
=== Default value ===
dataclasses: 0.2395797820063308 seconds
dataclassy: 0.25323228500201367 seconds
dataclassy (slots): 0.20265625597676262 seconds
"""
from typing import Dict
from timeit import timeit
import dataclasses
import dataclassy
def heading(text: str, decoration: str = '==='):
"""Print a heading."""
print('\n', decoration, text, decoration)
def result(label: str, expression: str):
"""Time an expression and print the result, along with a label."""
timing = timeit(expression, globals=globals())
print(f'{label}: {timing} seconds')
heading('Simple class')
@dataclasses.dataclass
class DsSimple:
a: int
b: int
@dataclassy.dataclass
class DySimple:
a: int
b: int
@dataclassy.dataclass(slots=True)
class DySimpleSlots:
a: int
b: int
result('dataclasses', 'DsSimple(1, 2)')
result('dataclassy', 'DySimple(1, 2)')
result('dataclassy (slots)', 'DySimpleSlots(1, 2)')
heading('Default value')
@dataclasses.dataclass
class DsDefault:
c: Dict = dataclasses.field(default_factory=dict)
@dataclassy.dataclass
class DyDefault:
c: Dict = {}
@dataclassy.dataclass(slots=True)
class DyDefaultSlots:
c: Dict = {}
result('dataclasses', 'DsDefault()')
result('dataclassy', 'DyDefault()')
result('dataclassy (slots)', 'DyDefaultSlots()')