codeflash-agent/.codeflash/krrt7/textualize/rich/bench/bench_runtime.py
Kevin Turcios 20f6c59f05
Lint and format entire repo, not just packages (#23)
Remove .codeflash/ from ruff extend-exclude, add per-file ignores
for .codeflash/, scripts/, evals/, and plugin/ (benchmark/script
patterns like print, eval, magic values). Remove shebangs. Widen
pre-commit hooks to check the full repo.
2026-04-15 03:16:15 -05:00

96 lines
2.3 KiB
Python

"""Benchmark the runtime optimizations in PR #12.
Compares:
- ConsoleOptions.__eq__ (explicit short-circuit vs all(getattr(...)))
- ConsoleOptions.update() (identity check vs isinstance)
- _emoji_replace() (inline cache vs _get_emoji() call)
Usage:
python3.13 bench_runtime.py
"""
import os
import sys
import timeit
sys.path.insert(0, os.path.expanduser("~/rich"))
def bench(label, stmt, setup, number=500_000):
times = timeit.repeat(stmt, setup, number=number, repeat=5)
best = min(times)
per_call_ns = best / number * 1e9
print(
f" {label}: {best * 1000:.1f}ms total, {per_call_ns:.0f}ns/call ({number:,} iterations, best of 5)"
)
return best
print(f"Python {sys.version}")
print(f"Rich path: {os.path.expanduser('~/rich')}")
print()
# --- 1. ConsoleOptions.__eq__ ---
print("=== ConsoleOptions.__eq__ ===")
eq_setup = """\
import sys, os
sys.path.insert(0, os.path.expanduser("~/rich"))
from rich.console import Console
c = Console()
opts_a = c.options
opts_b = c.options.copy()
"""
bench("__eq__ (equal objects)", "opts_a == opts_b", eq_setup)
bench("__eq__ (same object)", "opts_a == opts_a", eq_setup)
eq_setup_diff = (
eq_setup
+ """\
from rich.console import ConsoleDimensions
opts_c = opts_b.copy()
opts_c.size = ConsoleDimensions(999, 999)
"""
)
bench("__eq__ (differ at size)", "opts_a == opts_c", eq_setup_diff)
print()
# --- 2. ConsoleOptions.update() ---
print("=== ConsoleOptions.update() ===")
update_setup = """\
import sys, os
sys.path.insert(0, os.path.expanduser("~/rich"))
from rich.console import Console
c = Console()
opts = c.options
"""
bench("update(width=80)", "opts.update(width=80)", update_setup)
bench("update() no changes", "opts.update()", update_setup)
bench(
"update(width=80, no_wrap=True, highlight=False)",
"opts.update(width=80, no_wrap=True, highlight=False)",
update_setup,
)
print()
# --- 3. _emoji_replace ---
print("=== _emoji_replace() ===")
emoji_setup = """\
import sys, os
sys.path.insert(0, os.path.expanduser("~/rich"))
from rich._emoji_replace import _emoji_replace
"""
bench(
"_emoji_replace (with emoji)",
'_emoji_replace("Hello :wave: world :smile:")',
emoji_setup,
number=200_000,
)
bench(
"_emoji_replace (no emoji)",
'_emoji_replace("Hello world, no emojis here")',
emoji_setup,
number=200_000,
)
print()
print("Done.")