Skip to content

Commit 2082c53

Browse files
add async_generators benchmark (#230)
1 parent 81af6f2 commit 2082c53

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

pyperformance/data-files/benchmarks/MANIFEST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
name metafile
44
2to3 <local>
5+
async_generators <local>
56
async_tree <local>
67
async_tree_cpu_io_mixed <local:async_tree>
78
async_tree_io <local:async_tree>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[project]
2+
name = "pyperformance_bm_async_generators"
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 = "async_generators"
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
Benchmark recursive async generators implemented in python
3+
by traversing a binary tree.
4+
5+
Author: Kumar Aditya
6+
"""
7+
8+
from __future__ import annotations
9+
10+
from collections.abc import AsyncIterator
11+
12+
import pyperf
13+
14+
15+
class Tree:
16+
def __init__(self, left: Tree | None, value: int, right: Tree | None) -> None:
17+
self.left = left
18+
self.value = value
19+
self.right = right
20+
21+
async def __aiter__(self) -> AsyncIterator[int]:
22+
if self.left:
23+
async for i in self.left:
24+
yield i
25+
yield self.value
26+
if self.right:
27+
async for i in self.right:
28+
yield i
29+
30+
31+
def tree(input: range) -> Tree | None:
32+
n = len(input)
33+
if n == 0:
34+
return None
35+
i = n // 2
36+
return Tree(tree(input[:i]), input[i], tree(input[i + 1:]))
37+
38+
async def bench_async_generators() -> None:
39+
async for _ in tree(range(100000)):
40+
pass
41+
42+
if __name__ == "__main__":
43+
runner = pyperf.Runner()
44+
runner.metadata['description'] = "Benchmark async generators"
45+
runner.bench_async_func('async_generators', bench_async_generators)

0 commit comments

Comments
 (0)