mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 03:04:38 +08:00
Add dependency-free CPU-bound workloads adapted from pyperformance 1.14.0 to cover interpreter, standard-library serialization, regex, numeric, and garbage-collection paths under the existing Criterion and CodSpeed harnesses. Keep inputs deterministic and scale expensive cases for simulation while checking that benchmark execution does not leak interpreter state. Assisted-by: Codex:gpt-5.6-sol
43 lines
1017 B
Python
43 lines
1017 B
Python
# Adapted from python/pyperformance 1.14.0 (generators).
|
|
"""
|
|
Benchmark recursive generators implemented in python
|
|
by traversing a binary tree.
|
|
|
|
Author: Kumar Aditya
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Iterator
|
|
|
|
class Tree:
|
|
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]:
|
|
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(tree(input[:i]), input[i], tree(input[i + 1:]))
|
|
|
|
def bench_generators(loops: int) -> None:
|
|
assert list(tree(range(10))) == list(range(10))
|
|
range_it = range(loops)
|
|
iterable = tree(range(100000))
|
|
for _ in range_it:
|
|
for _ in iterable:
|
|
pass
|
|
|
|
bench_generators(1)
|