Repository navigation
Expand file tree
/
Copy pathbench_alpha_lib.php
More file actions
46 lines (45 loc) · 2.3 KB
/
Copy pathbench_alpha_lib.php
File metadata and controls
46 lines (45 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
/* Shared helper for the Zend/bench_*_alpha.php scripts.
*
* Order-alternated microbenchmark harness. Each bench() call warms up, then
* runs an EVEN number of order-alternated repeats of `iters` invocations of
* $base and $alpha, and reports the BEST (minimum) time for each arm. It prints
* a single line the Zend/alpha_guard.sh harness parses:
*
* <label> iters=<N> base=<ns> ns alpha=<ns> ns ratio=<base/alpha>x speedup=<%>
*
* ratio > 1.0 means the _alpha variant is faster.
*
* Why min-of-N (not median), and why an EVEN repeat count: on a thermally
* constrained host (e.g. a Raspberry Pi), the arm that runs SECOND in a pass
* runs on a warmer SoC and is biased slower. With an odd repeat count the
* order alternation is unbalanced (one arm runs second more often), which
* systematically penalises that arm. Using an EVEN count (equal first/second
* splits) plus the per-arm MINIMUM (the coolest pass, least throttled)
* cancels that bias: each arm's reported time is its least-throttled run.
* require this from a bench script with: require __DIR__ . '/bench_alpha_lib.php';
*/
function bench(string $label, callable $base, callable $alpha, int $iters): void {
for ($i = 0; $i < 50; $i++) { $base(); $alpha(); }
$repeats = 8;
$baseTimes = []; $alphaTimes = [];
for ($r = 0; $r < $repeats; $r++) {
if (($r & 1) === 0) {
$t0 = hrtime(true); for ($i = 0; $i < $iters; $i++) $base(); $tb = hrtime(true) - $t0;
$t0 = hrtime(true); for ($i = 0; $i < $iters; $i++) $alpha(); $ta = hrtime(true) - $t0;
} else {
$t0 = hrtime(true); for ($i = 0; $i < $iters; $i++) $alpha(); $ta = hrtime(true) - $t0;
$t0 = hrtime(true); for ($i = 0; $i < $iters; $i++) $base(); $tb = hrtime(true) - $t0;
}
$baseTimes[] = $tb; $alphaTimes[] = $ta;
}
sort($baseTimes); sort($alphaTimes);
/* min per arm: the least-throttled (coolest) pass, which removes the
* second-arm-warms-the-SoC bias. [0] is the smallest after sort(). */
$bm = $baseTimes[0];
$am = $alphaTimes[0];
$ratio = ($am > 0) ? ($bm / $am) : 0;
$speedup = ($bm > 0) ? (100 * (1 - $am / $bm)) : 0;
printf("%-52s iters=%-6d base=%10.0f ns alpha=%10.0f ns ratio=%5.2fx speedup=%+6.1f%%\n",
$label, $iters, $bm, $am, $ratio, $speedup);
}