From bc3c5448478b767be4aafde70ebfebe10169f451 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Wed, 18 May 2022 11:26:46 +0000 Subject: [PATCH 1/3] Add generators benchmarks --- pyperformance/data-files/benchmarks/MANIFEST | 1 + .../benchmarks/bm_generators/pyproject.toml | 9 ++++ .../benchmarks/bm_generators/run_benchmark.py | 47 +++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 pyperformance/data-files/benchmarks/bm_generators/pyproject.toml create mode 100644 pyperformance/data-files/benchmarks/bm_generators/run_benchmark.py diff --git a/pyperformance/data-files/benchmarks/MANIFEST b/pyperformance/data-files/benchmarks/MANIFEST index 81bafb96..38cd40b5 100644 --- a/pyperformance/data-files/benchmarks/MANIFEST +++ b/pyperformance/data-files/benchmarks/MANIFEST @@ -6,6 +6,7 @@ async_tree async_tree_cpu_io_mixed async_tree_io async_tree_memoization +generators chameleon chaos crypto_pyaes diff --git a/pyperformance/data-files/benchmarks/bm_generators/pyproject.toml b/pyperformance/data-files/benchmarks/bm_generators/pyproject.toml new file mode 100644 index 00000000..15211a3e --- /dev/null +++ b/pyperformance/data-files/benchmarks/bm_generators/pyproject.toml @@ -0,0 +1,9 @@ +[project] +name = "pyperformance_bm_generators" +requires-python = ">=3.8" +dependencies = ["pyperf"] +urls = {repository = "https://github.com/python/pyperformance"} +dynamic = ["version"] + +[tool.pyperformance] +name = "generators" \ No newline at end of file diff --git a/pyperformance/data-files/benchmarks/bm_generators/run_benchmark.py b/pyperformance/data-files/benchmarks/bm_generators/run_benchmark.py new file mode 100644 index 00000000..c6d93a93 --- /dev/null +++ b/pyperformance/data-files/benchmarks/bm_generators/run_benchmark.py @@ -0,0 +1,47 @@ +""" +Benchmark recursive generators implemented in python +by traversing a binary tree. + +Author: Kumar Aditya +""" + +from __future__ import annotations + +from collections.abc import Iterator + +import pyperf + + +class Tree: + def __init__(self, value: int, left: Tree | None, right: Tree | None) -> None: + self.value = value + self.left = left + self.right = right + + def __iter__(self) -> Iterator[int]: + if self.left: + yield from self.left + yield self.value + if self.right: + yield from self.right + + +def tree(input: range) -> Tree | None: + n = len(input) + if n == 0: + return None + i = n // 2 + return Tree(input[i], tree(input[:i]), tree(input[i + 1:])) + +def bench_generators(loops: int) -> None: + range_it = range(loops) + t0 = pyperf.perf_counter() + for _ in range_it: + for _ in tree(range(100000)): + pass + return pyperf.perf_counter() - t0 + +if __name__ == "__main__": + runner = pyperf.Runner() + runner.metadata['description'] = "Benchmark generators" + runner.bench_time_func('generators', bench_generators) \ No newline at end of file From 40a052d7ee7df45fcaeaf891d2f6e12b916386ec Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Sun, 22 May 2022 16:11:35 +0000 Subject: [PATCH 2/3] add assert --- .../data-files/benchmarks/bm_generators/run_benchmark.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pyperformance/data-files/benchmarks/bm_generators/run_benchmark.py b/pyperformance/data-files/benchmarks/bm_generators/run_benchmark.py index c6d93a93..7e082b4b 100644 --- a/pyperformance/data-files/benchmarks/bm_generators/run_benchmark.py +++ b/pyperformance/data-files/benchmarks/bm_generators/run_benchmark.py @@ -34,6 +34,7 @@ def tree(input: range) -> Tree | None: return Tree(input[i], tree(input[:i]), tree(input[i + 1:])) def bench_generators(loops: int) -> None: + assert list(tree(range(10))) == list(range(10)) range_it = range(loops) t0 = pyperf.perf_counter() for _ in range_it: From a3a3b602b270933dc0809d3142a5ffb4a8df865d Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Tue, 24 May 2022 05:23:28 +0000 Subject: [PATCH 3/3] code review --- .../data-files/benchmarks/bm_generators/pyproject.toml | 2 +- .../data-files/benchmarks/bm_generators/run_benchmark.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pyperformance/data-files/benchmarks/bm_generators/pyproject.toml b/pyperformance/data-files/benchmarks/bm_generators/pyproject.toml index 15211a3e..c49a70e3 100644 --- a/pyperformance/data-files/benchmarks/bm_generators/pyproject.toml +++ b/pyperformance/data-files/benchmarks/bm_generators/pyproject.toml @@ -6,4 +6,4 @@ urls = {repository = "https://github.com/python/pyperformance"} dynamic = ["version"] [tool.pyperformance] -name = "generators" \ No newline at end of file +name = "generators" diff --git a/pyperformance/data-files/benchmarks/bm_generators/run_benchmark.py b/pyperformance/data-files/benchmarks/bm_generators/run_benchmark.py index 7e082b4b..dcd4f669 100644 --- a/pyperformance/data-files/benchmarks/bm_generators/run_benchmark.py +++ b/pyperformance/data-files/benchmarks/bm_generators/run_benchmark.py @@ -13,9 +13,9 @@ class Tree: - def __init__(self, value: int, left: Tree | None, right: Tree | None) -> None: - self.value = value + def __init__(self, left: Tree | None, value: int, right: Tree | None) -> None: self.left = left + self.value = value self.right = right def __iter__(self) -> Iterator[int]: @@ -31,7 +31,7 @@ def tree(input: range) -> Tree | None: if n == 0: return None i = n // 2 - return Tree(input[i], tree(input[:i]), tree(input[i + 1:])) + return Tree(tree(input[:i]), input[i], tree(input[i + 1:])) def bench_generators(loops: int) -> None: assert list(tree(range(10))) == list(range(10)) @@ -45,4 +45,4 @@ def bench_generators(loops: int) -> None: if __name__ == "__main__": runner = pyperf.Runner() runner.metadata['description'] = "Benchmark generators" - runner.bench_time_func('generators', bench_generators) \ No newline at end of file + runner.bench_time_func('generators', bench_generators)