Skip to content

Commit 259edee

Browse files
Add benchmark for deepcopy (#201)
* Add benchmark for deepcopy * fix typo * Update pyperformance/data-files/benchmarks/bm_deepcopy/run_benchmark.py Co-authored-by: Eric Snow <[email protected]> * Update pyperformance/data-files/benchmarks/bm_deepcopy/run_benchmark.py Co-authored-by: Eric Snow <[email protected]> * address review comments * Update pyperformance/data-files/benchmarks/bm_deepcopy/run_benchmark.py Co-authored-by: Eric Snow <[email protected]> Co-authored-by: Eric Snow <[email protected]>
1 parent 01d3e81 commit 259edee

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

doc/benchmarks.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ See `pyaes <https://github.com/ricmoo/pyaes>`_: A pure-Python implementation of
124124
the AES block cipher algorithm and the common modes of operation (CBC, CFB,
125125
CTR, ECB and OFB).
126126

127+
deepcopy
128+
--------
129+
130+
Benchmark the Python `copy.deepcopy` method. The `deepcopy` method is
131+
performed on a nested dictionary and a dataclass.
127132

128133
deltablue
129134
---------

pyperformance/data-files/benchmarks/MANIFEST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ generators <local>
1111
chameleon <local>
1212
chaos <local>
1313
crypto_pyaes <local>
14+
deepcopy <local>
1415
deltablue <local>
1516
django_template <local>
1617
dulwich_log <local>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[project]
2+
name = "pyperformance_bm_deepcopy"
3+
requires-python = ">=3.8"
4+
dependencies = ["pyperf"]
5+
urls = {repository = "https://github.com/python/pyperformance"}
6+
dynamic = ["version"]
7+
8+
[tool.pyperformance]
9+
name = "deepcopy"
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
"""
2+
Benchmark to measure performance of the python builtin method copy.deepcopy
3+
4+
Performance is tested on a nested dictionary and a dataclass
5+
6+
Author: Pieter Eendebak
7+
8+
"""
9+
import copy
10+
import pyperf
11+
from dataclasses import dataclass
12+
13+
14+
@dataclass
15+
class A:
16+
string: str
17+
lst: list
18+
boolean: bool
19+
20+
21+
def benchmark_reduce(n):
22+
""" Benchmark where the __reduce__ functionality is used """
23+
class C(object):
24+
def __init__(self):
25+
self.a = 1
26+
self.b = 2
27+
28+
def __reduce__(self):
29+
return (C, (), self.__dict__)
30+
31+
def __setstate__(self, state):
32+
self.__dict__.update(state)
33+
c = C()
34+
35+
t0 = pyperf.perf_counter()
36+
for ii in range(n):
37+
_ = copy.deepcopy(c)
38+
dt = pyperf.perf_counter() - t0
39+
return dt
40+
41+
42+
def benchmark_memo(n):
43+
""" Benchmark where the memo functionality is used """
44+
A = [1] * 100
45+
data = {'a': (A, A, A), 'b': [A] * 100}
46+
47+
t0 = pyperf.perf_counter()
48+
for ii in range(n):
49+
_ = copy.deepcopy(data)
50+
dt = pyperf.perf_counter() - t0
51+
return dt
52+
53+
54+
def benchmark(n):
55+
""" Benchmark on some standard data types """
56+
a = {
57+
'list': [1, 2, 3, 43],
58+
't': (1 ,2, 3),
59+
'str': 'hello',
60+
'subdict': {'a': True}
61+
}
62+
dc = A('hello', [1, 2, 3], True)
63+
64+
dt = 0
65+
for ii in range(n):
66+
for jj in range(30):
67+
t0 = pyperf.perf_counter()
68+
_ = copy.deepcopy(a)
69+
dt += pyperf.perf_counter() - t0
70+
for s in ['red', 'blue', 'green']:
71+
dc.string = s
72+
for kk in range(5):
73+
dc.lst[0] = kk
74+
for b in [True, False]:
75+
dc.boolean = b
76+
t0 = pyperf.perf_counter()
77+
_ = copy.deepcopy(dc)
78+
dt += pyperf.perf_counter() - t0
79+
return dt
80+
81+
82+
if __name__ == "__main__":
83+
runner = pyperf.Runner()
84+
runner.metadata['description'] = "deepcopy benchmark"
85+
86+
runner.bench_time_func('deepcopy', benchmark)
87+
runner.bench_time_func('deepcopy_reduce', benchmark_reduce)
88+
runner.bench_time_func('deepcopy_memo', benchmark_memo)

0 commit comments

Comments
 (0)