"""Counterbalanced timings and macOS physical footprint, without another GPU runtime.""" from __future__ import annotations import statistics import time import mlx.core as mx def paired(control, candidate, repeats=6, warmup=2): for _ in range(warmup): mx.eval(control()) mx.eval(candidate()) samples = {"control": [], "candidate": []} order = [] for i in range(repeats): names = ("control", "candidate") if i % 2 == 0 else ("candidate", "control") for name in names: fn = control if name == "control" else candidate mx.synchronize() start = time.perf_counter() value = fn() mx.eval(value) mx.synchronize() samples[name].append((time.perf_counter() - start) * 1000) order.append(name) del value result = {"order": order} for name, values in samples.items(): result[name] = { "median_ms": statistics.median(values), "min_ms": min(values), "max_ms": max(values), "samples_ms": values, } result["reduction_pct"] = 100 * ( 1 - result["candidate"]["median_ms"] / result["control"]["median_ms"] ) return result