1
0

Add exploratory timing harness

This commit is contained in:
2026-07-26 11:45:07 +00:00
parent e4695ab334
commit 82cccd62c0

View File

@@ -0,0 +1,41 @@
package com.ankurm.vectorapi;
import java.util.Arrays;
/**
* A deliberately small timing harness. It is NOT a statistical benchmark:
* it warms the JIT up, then reports the median and best of several rounds so
* that large effects (2x and up) are visible. Use JMH for anything you intend
* to defend -- see the jmh/ module in this repository.
*/
public final class Bench {
public static final int WARMUP_CALLS = 50_000;
public static final int ROUND_CALLS = 5_000;
public static final int ROUNDS = 15;
public interface Kernel { void run(); }
/** @return median ns/call over ROUNDS rounds (also prints best). */
public static double measure(String label, Kernel k) {
for (int i = 0; i < WARMUP_CALLS; i++) k.run(); // force C2 compilation
double[] perCall = new double[ROUNDS];
for (int r = 0; r < ROUNDS; r++) {
long t0 = System.nanoTime();
for (int i = 0; i < ROUND_CALLS; i++) k.run();
long t1 = System.nanoTime();
perCall[r] = (t1 - t0) / (double) ROUND_CALLS;
}
double[] sorted = perCall.clone();
Arrays.sort(sorted);
double median = sorted[ROUNDS / 2];
double best = sorted[0];
System.out.printf("%-24s median %8.1f ns/call best %8.1f ns/call%n", label, median, best);
return median;
}
public static void speedup(String label, double scalarNs, double vectorNs) {
System.out.printf("%-24s %.2fx%n", label, scalarNs / vectorNs);
}
private Bench() {}
}