Skip to content

Commit 053d36f

Browse files
Add coroutines benchmark (#217)
1 parent 4ccd8db commit 053d36f

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

pyperformance/data-files/benchmarks/MANIFEST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ async_tree <local>
66
async_tree_cpu_io_mixed <local:async_tree>
77
async_tree_io <local:async_tree>
88
async_tree_memoization <local:async_tree>
9+
coroutines <local>
910
coverage <local>
1011
generators <local>
1112
chameleon <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_coroutines"
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 = "coroutines"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
Benchmark for recursive coroutines.
3+
4+
Author: Kumar Aditya
5+
"""
6+
7+
import pyperf
8+
9+
10+
async def fibonacci(n: int) -> int:
11+
if n <= 1:
12+
return n
13+
return await fibonacci(n - 1) + await fibonacci(n - 2)
14+
15+
16+
def bench_coroutines(loops: int) -> float:
17+
range_it = range(loops)
18+
t0 = pyperf.perf_counter()
19+
for _ in range_it:
20+
coro = fibonacci(25)
21+
try:
22+
while True:
23+
coro.send(None)
24+
except StopIteration:
25+
pass
26+
return pyperf.perf_counter() - t0
27+
28+
29+
if __name__ == "__main__":
30+
runner = pyperf.Runner()
31+
runner.metadata['description'] = "Benchmark coroutines"
32+
runner.bench_time_func('coroutines', bench_coroutines)

0 commit comments

Comments
 (0)