-
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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]>
- Loading branch information
1 parent
01d3e81
commit 259edee
Showing
4 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
pyperformance/data-files/benchmarks/bm_deepcopy/pyproject.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[project] | ||
name = "pyperformance_bm_deepcopy" | ||
requires-python = ">=3.8" | ||
dependencies = ["pyperf"] | ||
urls = {repository = "https://github.com/python/pyperformance"} | ||
dynamic = ["version"] | ||
|
||
[tool.pyperformance] | ||
name = "deepcopy" |
88 changes: 88 additions & 0 deletions
88
pyperformance/data-files/benchmarks/bm_deepcopy/run_benchmark.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
""" | ||
Benchmark to measure performance of the python builtin method copy.deepcopy | ||
Performance is tested on a nested dictionary and a dataclass | ||
Author: Pieter Eendebak | ||
""" | ||
import copy | ||
import pyperf | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class A: | ||
string: str | ||
lst: list | ||
boolean: bool | ||
|
||
|
||
def benchmark_reduce(n): | ||
""" Benchmark where the __reduce__ functionality is used """ | ||
class C(object): | ||
def __init__(self): | ||
self.a = 1 | ||
self.b = 2 | ||
|
||
def __reduce__(self): | ||
return (C, (), self.__dict__) | ||
|
||
def __setstate__(self, state): | ||
self.__dict__.update(state) | ||
c = C() | ||
|
||
t0 = pyperf.perf_counter() | ||
for ii in range(n): | ||
_ = copy.deepcopy(c) | ||
dt = pyperf.perf_counter() - t0 | ||
return dt | ||
|
||
|
||
def benchmark_memo(n): | ||
""" Benchmark where the memo functionality is used """ | ||
A = [1] * 100 | ||
data = {'a': (A, A, A), 'b': [A] * 100} | ||
|
||
t0 = pyperf.perf_counter() | ||
for ii in range(n): | ||
_ = copy.deepcopy(data) | ||
dt = pyperf.perf_counter() - t0 | ||
return dt | ||
|
||
|
||
def benchmark(n): | ||
""" Benchmark on some standard data types """ | ||
a = { | ||
'list': [1, 2, 3, 43], | ||
't': (1 ,2, 3), | ||
'str': 'hello', | ||
'subdict': {'a': True} | ||
} | ||
dc = A('hello', [1, 2, 3], True) | ||
|
||
dt = 0 | ||
for ii in range(n): | ||
for jj in range(30): | ||
t0 = pyperf.perf_counter() | ||
_ = copy.deepcopy(a) | ||
dt += pyperf.perf_counter() - t0 | ||
for s in ['red', 'blue', 'green']: | ||
dc.string = s | ||
for kk in range(5): | ||
dc.lst[0] = kk | ||
for b in [True, False]: | ||
dc.boolean = b | ||
t0 = pyperf.perf_counter() | ||
_ = copy.deepcopy(dc) | ||
dt += pyperf.perf_counter() - t0 | ||
return dt | ||
|
||
|
||
if __name__ == "__main__": | ||
runner = pyperf.Runner() | ||
runner.metadata['description'] = "deepcopy benchmark" | ||
|
||
runner.bench_time_func('deepcopy', benchmark) | ||
runner.bench_time_func('deepcopy_reduce', benchmark_reduce) | ||
runner.bench_time_func('deepcopy_memo', benchmark_memo) |