@@ -198,14 +198,75 @@ def measure(args: argparse.Namespace) -> int:
198198 return 0
199199
200200
201+ def interleaved (args : argparse .Namespace ) -> int :
202+ """CPython vs TypedPython @compiled, alternated A/B in the same time window (leader decision,
203+ 2026-10-03): when the machine is never quiet, load hits both sides of each pair alike, so the
204+ per-pair ratio is stable even though absolute times are not. Reports the median ratio, never
205+ absolute times as the result, with the load average recorded at every pair."""
206+ if not args .typedpython :
207+ print ("interleaved needs --typedpython" )
208+ return 2
209+ compiled = build_compiled (Path (args .typedpython ), args .typedpython_python )
210+ results : dict [str , dict [str , object ]] = {}
211+ for name , spec in BENCHMARKS .items ():
212+ entry = compiled .get (name )
213+ if not isinstance (entry , tuple ):
214+ results [name ] = {"skipped" : entry }
215+ continue
216+ size = args .size .get (name , spec [5 ])
217+ cmds = commands (name , size , args .python , False , compiled )
218+ a_cmd , b_cmd = cmds ["CPython" ], cmds ["TypedPython @compiled" ]
219+ pairs = []
220+ for i in range (args .pairs ):
221+ order = (a_cmd , b_cmd ) if i % 2 == 0 else (b_cmd , a_cmd ) # alternate which runs first
222+ load = os .getloadavg ()
223+ (t1 , o1 ), (t2 , o2 ) = run_once (order [0 ]), run_once (order [1 ])
224+ t_a , o_a , t_b , o_b = (t1 , o1 , t2 , o2 ) if order [0 ] is a_cmd else (t2 , o2 , t1 , o1 )
225+ if o_a != o_b :
226+ print (f"{ name } : OUTPUTS DIFFER in pair { i } : { o_a !r} vs { o_b !r} " )
227+ return 1
228+ pairs .append ({"cpython_s" : t_a , "compiled_s" : t_b , "ratio" : t_a / t_b , "load" : load })
229+ ratios = [p ["ratio" ] for p in pairs ]
230+ results [name ] = {"size" : size , "compiled_functions" : entry [1 ], "pairs" : pairs ,
231+ "median_ratio" : statistics .median (ratios ), "min_ratio" : min (ratios ),
232+ "max_ratio" : max (ratios )}
233+ print (f"measured { name } " , file = sys .stderr )
234+
235+ print ()
236+ print ("| benchmark | size | speedup vs CPython (median of pairs) | range | load during pairs (1-min) |" )
237+ print ("|---|---|---|---|---|" )
238+ for name , r in results .items ():
239+ if "skipped" in r :
240+ print (f"| { name } | — | { r ['skipped' ]} | | |" )
241+ continue
242+ loads = [p ["load" ][0 ] for p in r ["pairs" ]] # type: ignore[index]
243+ print (f"| { name } | { r ['size' ]} | { r ['median_ratio' ]:.1f} x | { r ['min_ratio' ]:.1f} –{ r ['max_ratio' ]:.1f} x "
244+ f"| { min (loads ):.1f} –{ max (loads ):.1f} |" )
245+ print (f"\n { args .pairs } A/B pairs per benchmark, order alternated; ratio = CPython time / compiled time." )
246+ if args .write :
247+ out_dir = ROOT / "results"
248+ out_dir .mkdir (exist_ok = True )
249+ path = out_dir / f"{ datetime .date .today ().isoformat ()} -{ socket .gethostname ()} -interleaved.json"
250+ path .write_text (json .dumps ({"date" : datetime .datetime .now ().isoformat (timespec = "seconds" ),
251+ "host" : socket .gethostname (), "method" : "interleaved A/B" ,
252+ "pairs" : args .pairs , "results" : results }, indent = 2 ) + "\n " )
253+ print (f"wrote { path } " )
254+ return 0
255+
256+
201257def main () -> int :
202258 parser = argparse .ArgumentParser (description = __doc__ , formatter_class = argparse .RawDescriptionHelpFormatter )
203259 sub = parser .add_subparsers (dest = "cmd" , required = True )
204260 v = sub .add_parser ("verify" )
205261 v .add_argument ("--python" , default = sys .executable )
206262 m = sub .add_parser ("measure" )
207263 m .add_argument ("--python" , default = sys .executable )
208- for p in (v , m ):
264+ ab = sub .add_parser ("interleaved" , help = "CPython vs @compiled alternated A/B; reports median ratios" )
265+ ab .add_argument ("--python" , default = sys .executable )
266+ ab .add_argument ("--pairs" , type = int , default = 11 )
267+ ab .add_argument ("--write" , action = "store_true" )
268+ ab .add_argument ("--size" , action = "append" , default = [], metavar = "NAME=N" )
269+ for p in (v , m , ab ):
209270 p .add_argument ("--typedpython" , metavar = "DIR" ,
210271 help = "directory holding the typedpython package; enables the @compiled column" )
211272 p .add_argument ("--typedpython-python" , default = sys .executable ,
@@ -216,8 +277,10 @@ def main() -> int:
216277 m .add_argument ("--size" , action = "append" , default = [], metavar = "NAME=N" ,
217278 help = "override a benchmark size (for runner smoke tests only)" )
218279 args = parser .parse_args ()
219- if args .cmd == "measure" :
280+ if args .cmd in ( "measure" , "interleaved" ) :
220281 args .size = dict (s .split ("=" , 1 ) for s in args .size )
282+ if args .cmd == "interleaved" :
283+ return interleaved (args )
221284 return verify (args ) if args .cmd == "verify" else measure (args )
222285
223286
0 commit comments