Skip to content

Commit 6bdbe65

Browse files
[Autoloop: perf-comparison] Iteration 391: Add bootstrap benchmark pair
Run: https://github.com/githubnext/tsb/actions/runs/29062236162 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 1f34210 commit 6bdbe65

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""Benchmark: bootstrap confidence interval on 1000-element array
2+
Uses percentile method with 500 resamples for a realistic workload.
3+
"""
4+
import json
5+
import time
6+
import numpy as np
7+
8+
N = 1_000
9+
WARMUP = 3
10+
ITERATIONS = 10
11+
12+
rng = np.random.default_rng(42)
13+
data = np.sin(np.arange(N) * 0.01) * 50 + 100
14+
15+
16+
def bootstrap_ci(arr, stat_fn, n_resamples=500, seed=42):
17+
"""Percentile bootstrap CI."""
18+
rng_local = np.random.default_rng(seed)
19+
stats = np.empty(n_resamples)
20+
for i in range(n_resamples):
21+
resample = rng_local.choice(arr, size=len(arr), replace=True)
22+
stats[i] = stat_fn(resample)
23+
return np.percentile(stats, [2.5, 97.5])
24+
25+
26+
def mean_fn(xs):
27+
return np.mean(xs)
28+
29+
30+
for _ in range(WARMUP):
31+
bootstrap_ci(data, mean_fn, n_resamples=500)
32+
33+
start = time.perf_counter()
34+
for _ in range(ITERATIONS):
35+
bootstrap_ci(data, mean_fn, n_resamples=500)
36+
total = (time.perf_counter() - start) * 1000
37+
38+
print(json.dumps({
39+
"function": "bootstrap",
40+
"mean_ms": total / ITERATIONS,
41+
"iterations": ITERATIONS,
42+
"total_ms": total,
43+
}))

benchmarks/tsb/bench_bootstrap.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Benchmark: bootstrap confidence interval on 1000-element array
3+
* Uses percentile method with 500 resamples for a realistic workload.
4+
*/
5+
import { bootstrap1 } from "../../src/index.js";
6+
7+
const N = 1_000;
8+
const WARMUP = 3;
9+
const ITERATIONS = 10;
10+
11+
const data = Float64Array.from({ length: N }, (_, i) => Math.sin(i * 0.01) * 50 + 100);
12+
const arr = Array.from(data);
13+
14+
const mean = (xs: readonly number[]) => xs.reduce((a, b) => a + b, 0) / xs.length;
15+
16+
for (let i = 0; i < WARMUP; i++) {
17+
bootstrap1(arr, mean, { n: 500, method: "percentile", seed: 42 });
18+
}
19+
20+
const start = performance.now();
21+
for (let i = 0; i < ITERATIONS; i++) {
22+
bootstrap1(arr, mean, { n: 500, method: "percentile", seed: 42 });
23+
}
24+
const total = performance.now() - start;
25+
26+
console.log(
27+
JSON.stringify({
28+
function: "bootstrap",
29+
mean_ms: total / ITERATIONS,
30+
iterations: ITERATIONS,
31+
total_ms: total,
32+
}),
33+
);

0 commit comments

Comments
 (0)