mirror of
https://github.com/codeflash-ai/codeflash-agent.git
synced 2026-05-04 18:25:19 +00:00
99 lines
3.3 KiB
Python
99 lines
3.3 KiB
Python
"""Benchmark the runtime micro-optimizations from 6b354159.
|
|
|
|
Targets:
|
|
1. Style.__eq__ identity shortcut
|
|
2. Style.combine/chain via _add (bypassing sum + __add__)
|
|
3. Segment.simplify with `is` check
|
|
|
|
Usage:
|
|
cd ~/rich && ~/venv313/bin/python ~/bench/bench_runtime2.py
|
|
"""
|
|
import timeit
|
|
import sys
|
|
import os
|
|
|
|
sys.path.insert(0, os.path.expanduser("~/rich"))
|
|
|
|
def bench(label, stmt, setup, number=500_000, repeat=7):
|
|
times = timeit.repeat(stmt, setup, number=number, repeat=repeat)
|
|
best = min(times)
|
|
per_call_ns = best / number * 1e9
|
|
print(f" {label}: {best*1000:.1f}ms/{number//1000}K calls, {per_call_ns:.0f}ns/call")
|
|
return best
|
|
|
|
print(f"Python {sys.version}")
|
|
print()
|
|
|
|
common_setup = """\
|
|
import sys, os
|
|
sys.path.insert(0, os.path.expanduser("~/rich"))
|
|
from rich.style import Style
|
|
"""
|
|
|
|
# --- 1. Style.__eq__ ---
|
|
print("=== Style.__eq__ ===")
|
|
eq_setup = common_setup + """\
|
|
s1 = Style(bold=True, color="red")
|
|
s2 = Style(bold=True, color="red")
|
|
# Force hash caching
|
|
hash(s1); hash(s2)
|
|
"""
|
|
bench("identity (s1 == s1)", "s1 == s1", eq_setup, number=1_000_000)
|
|
bench("equal (s1 == s2)", "s1 == s2", eq_setup, number=1_000_000)
|
|
bench("not-equal (s1 != Style())", "s1 != Style()", eq_setup + "s3 = Style(); hash(s3)\n", number=1_000_000)
|
|
print()
|
|
|
|
# --- 2. Style.combine ---
|
|
print("=== Style.combine ===")
|
|
combine_setup = common_setup + """\
|
|
styles = [Style(bold=True), Style(color="red"), Style(italic=True)]
|
|
"""
|
|
bench("combine(3 styles)", "Style.combine(styles)", combine_setup, number=200_000)
|
|
|
|
combine_setup_2 = common_setup + """\
|
|
styles = [Style(bold=True), Style(color="red")]
|
|
"""
|
|
bench("combine(2 styles)", "Style.combine(styles)", combine_setup_2, number=200_000)
|
|
print()
|
|
|
|
# --- 3. Style.chain ---
|
|
print("=== Style.chain ===")
|
|
chain_setup = common_setup + """\
|
|
s1 = Style(bold=True)
|
|
s2 = Style(color="red")
|
|
s3 = Style(italic=True)
|
|
"""
|
|
bench("chain(3 styles)", "Style.chain(s1, s2, s3)", chain_setup, number=200_000)
|
|
print()
|
|
|
|
# --- 4. Segment.simplify ---
|
|
print("=== Segment.simplify ===")
|
|
simplify_setup = common_setup + """\
|
|
from rich.segment import Segment
|
|
style_a = Style(bold=True, color="red")
|
|
# Same object reference (common case)
|
|
segs_identity = [Segment("hello ", style_a), Segment("world", style_a), Segment("! ", style_a)]
|
|
# Equal but different objects
|
|
style_b = Style(bold=True, color="red")
|
|
segs_equal = [Segment("hello ", style_a), Segment("world", style_b), Segment("! ", style_a)]
|
|
# Different styles (no merge)
|
|
style_c = Style(italic=True)
|
|
segs_diff = [Segment("hello ", style_a), Segment("world", style_c), Segment("! ", style_a)]
|
|
"""
|
|
bench("simplify (identity styles)", "list(Segment.simplify(segs_identity))", simplify_setup, number=200_000)
|
|
bench("simplify (equal styles)", "list(Segment.simplify(segs_equal))", simplify_setup, number=200_000)
|
|
bench("simplify (diff styles)", "list(Segment.simplify(segs_diff))", simplify_setup, number=200_000)
|
|
print()
|
|
|
|
# --- 5. E2E Console.print ---
|
|
print("=== E2E Console.print ===")
|
|
e2e_setup = common_setup + """\
|
|
from rich.console import Console
|
|
from rich.text import Text
|
|
c = Console(file=open(os.devnull, "w"), color_system="truecolor")
|
|
markup = "[bold red]Error:[/bold red] Something [italic]went wrong[/italic] in [blue underline]module.py[/blue underline]:42"
|
|
"""
|
|
bench("Console.print(markup)", "c.print(markup)", e2e_setup, number=5_000, repeat=5)
|
|
print()
|
|
|
|
print("Done.")
|