From d9b950eb2f1bb695853e93adddbae47bfecc972a Mon Sep 17 00:00:00 2001 From: Satyam singh <106249269+Satyam12singh@users.noreply.github.com> Date: Sun, 28 Dec 2025 19:46:13 +0530 Subject: [PATCH] refactor: use time.perf_counter for consistent and accurate benchmarking (#2943) --- benchmarks/python/comparative/bench_mlx.py | 4 ++-- benchmarks/python/comparative/bench_torch.py | 4 ++-- benchmarks/python/time_utils.py | 4 ++-- docs/src/dev/extensions.rst | 4 ++-- examples/python/linear_regression.py | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/benchmarks/python/comparative/bench_mlx.py b/benchmarks/python/comparative/bench_mlx.py index 0821ccae..4e6ba04f 100644 --- a/benchmarks/python/comparative/bench_mlx.py +++ b/benchmarks/python/comparative/bench_mlx.py @@ -38,10 +38,10 @@ def bench(f, *args): for i in range(10): f(*args) - s = time.time() + s = time.perf_counter() for i in range(100): f(*args) - e = time.time() + e = time.perf_counter() return e - s diff --git a/benchmarks/python/comparative/bench_torch.py b/benchmarks/python/comparative/bench_torch.py index dd3436d9..f6e2ca5d 100644 --- a/benchmarks/python/comparative/bench_torch.py +++ b/benchmarks/python/comparative/bench_torch.py @@ -37,10 +37,10 @@ def bench(f, *args): for i in range(10): f(*args) - s = time.time() + s = time.perf_counter() for i in range(100): f(*args) - e = time.time() + e = time.perf_counter() return e - s diff --git a/benchmarks/python/time_utils.py b/benchmarks/python/time_utils.py index 2903c329..181f7d3f 100644 --- a/benchmarks/python/time_utils.py +++ b/benchmarks/python/time_utils.py @@ -31,8 +31,8 @@ def measure_runtime(fn, **kwargs): for _ in range(5): fn(**kwargs) - tic = time.time() + tic = time.perf_counter() iters = 100 for _ in range(iters): fn(**kwargs) - return (time.time() - tic) * 1000 / iters + return (time.perf_counter() - tic) * 1000 / iters diff --git a/docs/src/dev/extensions.rst b/docs/src/dev/extensions.rst index 8b5bd41e..392c7cbf 100644 --- a/docs/src/dev/extensions.rst +++ b/docs/src/dev/extensions.rst @@ -777,11 +777,11 @@ with the naive :meth:`simple_axpby` we first defined. mx.eval(z) # Timed run - s = time.time() + s = time.perf_counter() for i in range(100): z = f(x, y, alpha, beta) mx.eval(z) - e = time.time() + e = time.perf_counter() return 1000 * (e - s) / 100 simple_time = bench(simple_axpby) diff --git a/examples/python/linear_regression.py b/examples/python/linear_regression.py index c247d170..27b99565 100644 --- a/examples/python/linear_regression.py +++ b/examples/python/linear_regression.py @@ -29,12 +29,12 @@ def loss_fn(w): grad_fn = mx.grad(loss_fn) -tic = time.time() +tic = time.perf_counter() for _ in range(num_iters): grad = grad_fn(w) w = w - lr * grad mx.eval(w) -toc = time.time() +toc = time.perf_counter() loss = loss_fn(w) error_norm = mx.sum(mx.square(w - w_star)).item() ** 0.5