Skip to content

Commit 5d216e6

Browse files
hynekeendebakpt
andauthored
Add benchmarks for asdict and astuple (#1464)
In preparation for #1463 Co-authored-by: Pieter Eendebak <[email protected]>
1 parent ce358ed commit 5d216e6

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

bench/test_benchmarks.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,56 @@ def test_hash():
119119

120120
for _ in range(ROUNDS):
121121
hash(c)
122+
123+
124+
def test_asdict_complicated():
125+
"""
126+
Benchmark instances with non-shortcut fields.
127+
"""
128+
c = C()
129+
ad = attrs.asdict
130+
131+
for _ in range(ROUNDS):
132+
ad(c)
133+
134+
135+
def test_astuple_complicated():
136+
"""
137+
Benchmark instances with non-shortcut fields.
138+
"""
139+
c = C()
140+
at = attrs.astuple
141+
142+
for _ in range(ROUNDS):
143+
at(c)
144+
145+
146+
@attrs.define
147+
class AtomicFields:
148+
a: int = 0
149+
b: Ellipsis = ...
150+
c: str = "foo"
151+
d: tuple[str] = "bar"
152+
e: complex = complex()
153+
154+
155+
def test_asdict_atomic():
156+
"""
157+
Benchmark atomic-only instances.
158+
"""
159+
c = AtomicFields()
160+
ad = attrs.asdict
161+
162+
for _ in range(ROUNDS):
163+
ad(c)
164+
165+
166+
def test_astuple_atomic():
167+
"""
168+
Benchmark atomic-only instances.
169+
"""
170+
c = AtomicFields()
171+
at = attrs.astuple
172+
173+
for _ in range(ROUNDS):
174+
at(c)

0 commit comments

Comments
 (0)