@@ -51,10 +51,23 @@ def fmt_rsd(rsd: float | None) -> str:
5151
5252
5353def fmt_ns (seconds : float ) -> str :
54- ns = seconds * 1e9
55- if ns >= 1000 :
56- return f"{ ns / 1000 :.2f} us"
57- return f"{ ns :.0f} ns"
54+ """Format a duration in nanoseconds with a thousands separator.
55+
56+ Using a single unit across the whole table makes side-by-side comparison
57+ easier, even when some entries get into the multi-microsecond range.
58+ """
59+ return f"{ seconds * 1e9 :,.0f} "
60+
61+
62+ def fmt_overhead_ns (py_mean : float , cpp_mean : float ) -> str :
63+ return f"{ (py_mean - cpp_mean ) * 1e9 :+,.0f} "
64+
65+
66+ def fmt_overhead_pct (py_mean : float , cpp_mean : float ) -> str :
67+ if cpp_mean <= 0.0 :
68+ return "-"
69+ pct = (py_mean - cpp_mean ) / cpp_mean * 100
70+ return f"{ pct :+,.0f} %"
5871
5972
6073def main () -> None :
@@ -90,22 +103,29 @@ def main() -> None:
90103 name_width = max (len (n ) for n in all_names )
91104 name_width = max (name_width , len ("Benchmark" ))
92105
106+ # Right-aligned numeric columns. Widths chosen so header text fits and
107+ # multi-microsecond ns values with thousands separators still align.
108+ cpp_w = 12
109+ py_w = 12
110+ rsd_w = 8
111+ oh_ns_w = 12
112+ oh_pct_w = 10
113+
93114 # Header
94115 if cpp_benchmarks :
95116 header = (
96- f"{ 'Benchmark' :<{name_width }} { 'C++ (mean)' :>12} { 'C++ RSD' :>8} "
97- f"{ 'Python (mean)' :>14} { 'Py RSD' :>7} { 'Overhead' :>10} "
117+ f"{ 'Benchmark' :<{name_width }} "
118+ f"{ 'C++ (ns)' :>{cpp_w }} { 'C++ RSD' :>{rsd_w }} "
119+ f"{ 'Python (ns)' :>{py_w }} { 'Py RSD' :>{rsd_w }} "
120+ f"{ 'Overhead ns' :>{oh_ns_w }} { 'Overhead %' :>{oh_pct_w }} "
98121 )
99- sep = "-" * len (header )
100- print (sep )
101- print (header )
102- print (sep )
103122 else :
104- header = f"{ 'Benchmark' :<{name_width }} { 'Python (mean)' :>14} { 'Py RSD' :>7} "
105- sep = "-" * len (header )
106- print (sep )
107- print (header )
108- print (sep )
123+ header = f"{ 'Benchmark' :<{name_width }} { 'Python (ns)' :>{py_w }} { 'Py RSD' :>{rsd_w }} "
124+
125+ sep = "-" * len (header )
126+ print (sep )
127+ print (header )
128+ print (sep )
109129
110130 for name in all_names :
111131 py_vals = py_benchmarks .get (name )
@@ -120,17 +140,21 @@ def main() -> None:
120140 cpp_rsd = fmt_rsd (cpp_stats [2 ]) if cpp_stats else "-"
121141
122142 if py_stats and cpp_stats :
123- py_mean = py_stats [0 ]
124- cpp_mean = cpp_stats [0 ]
125- overhead_ns = (py_mean - cpp_mean ) * 1e9
126- overhead_str = f"{ overhead_ns :+.0f} ns"
143+ overhead_ns_str = fmt_overhead_ns (py_stats [0 ], cpp_stats [0 ])
144+ overhead_pct_str = fmt_overhead_pct (py_stats [0 ], cpp_stats [0 ])
127145 else :
128- overhead_str = "-"
146+ overhead_ns_str = "-"
147+ overhead_pct_str = "-"
129148
130149 if cpp_benchmarks :
131- print (f"{ name :<{name_width }} { cpp_str :>12} { cpp_rsd :>8} { py_str :>14} { py_rsd :>7} { overhead_str :>10} " )
150+ print (
151+ f"{ name :<{name_width }} "
152+ f"{ cpp_str :>{cpp_w }} { cpp_rsd :>{rsd_w }} "
153+ f"{ py_str :>{py_w }} { py_rsd :>{rsd_w }} "
154+ f"{ overhead_ns_str :>{oh_ns_w }} { overhead_pct_str :>{oh_pct_w }} "
155+ )
132156 else :
133- print (f"{ name :<{name_width }} { py_str :>14 } { py_rsd :>7 } " )
157+ print (f"{ name :<{name_width }} { py_str :>{ py_w } } { py_rsd :>{ rsd_w } } " )
134158
135159 print (sep )
136160
0 commit comments