Skip to content

Commit b4ab16a

Browse files
[Autoloop: perf-comparison] Iteration 385: add SparseArray benchmark pair; fix 2 broken Python benchmarks
- Add benchmarks/tsb/bench_sparse_array.ts and benchmarks/pandas/bench_sparse_array.py benchmarking SparseArray.fromDense / toDense / sum / mean on 100k-element 5%-density array - Fix escaped triple-quote docstrings in bench_str_extract_all.py and bench_str_extract_groups.py (re-emerged post-rebase; these caused evaluation to return null on every run) - Metric: 726 benchmark pairs (evaluation restored from null; +1 new pair) Run: https://github.com/githubnext/tsb/actions/runs/28663472320 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 8898a00 commit b4ab16a

4 files changed

Lines changed: 84 additions & 2 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""Benchmark: SparseArray fromDense / toDense / aggregations on 100k-element array (5% density)"""
2+
import json
3+
import time
4+
import numpy as np
5+
import pandas as pd
6+
7+
N = 100_000
8+
WARMUP = 3
9+
ITERATIONS = 10
10+
11+
# ~5% density: most values are 0, ~5k non-zero
12+
dense = np.zeros(N)
13+
for i in range(0, N, 20):
14+
dense[i] = np.sin(i * 0.001) * 100 + 1
15+
16+
# Pre-built sparse array for operations that don't test construction
17+
sparse = pd.arrays.SparseArray(dense, fill_value=0)
18+
19+
# Warm up
20+
for _ in range(WARMUP):
21+
pd.arrays.SparseArray(dense, fill_value=0)
22+
sparse.to_dense()
23+
sparse.sum()
24+
sparse.mean()
25+
26+
start = time.perf_counter()
27+
for _ in range(ITERATIONS):
28+
pd.arrays.SparseArray(dense, fill_value=0)
29+
sparse.to_dense()
30+
sparse.sum()
31+
sparse.mean()
32+
total = (time.perf_counter() - start) * 1000
33+
34+
print(json.dumps({
35+
"function": "sparse_array",
36+
"mean_ms": total / ITERATIONS,
37+
"iterations": ITERATIONS,
38+
"total_ms": total,
39+
}))

benchmarks/pandas/bench_str_extract_all.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
\"\"\"Benchmark: str.extractall on 10k-element string Series\"\"\"
1+
"""Benchmark: str.extractall on 10k-element string Series"""
22
import json, time
33
import pandas as pd
44

benchmarks/pandas/bench_str_extract_groups.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
\"\"\"Benchmark: str.extract on 10k-element string Series\"\"\"
1+
"""Benchmark: str.extract on 10k-element string Series"""
22
import json, time
33
import pandas as pd
44

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Benchmark: SparseArray fromDense / toDense / aggregations on 100k-element array (5% density)
3+
*/
4+
import { SparseArray } from "../../src/index.js";
5+
6+
const N = 100_000;
7+
const WARMUP = 3;
8+
const ITERATIONS = 10;
9+
10+
// ~5% density: most values are 0 (fill_value), 5k are non-zero
11+
const dense: number[] = new Array(N).fill(0);
12+
for (let i = 0; i < N; i += 20) {
13+
dense[i] = Math.sin(i * 0.001) * 100 + 1;
14+
}
15+
16+
// Pre-built sparse array for operations that don't test construction
17+
const sparse = SparseArray.fromDense(dense, 0, "float64");
18+
19+
// Warm up
20+
for (let i = 0; i < WARMUP; i++) {
21+
SparseArray.fromDense(dense, 0, "float64");
22+
sparse.toDense();
23+
sparse.sum();
24+
sparse.mean();
25+
}
26+
27+
const start = performance.now();
28+
for (let i = 0; i < ITERATIONS; i++) {
29+
SparseArray.fromDense(dense, 0, "float64");
30+
sparse.toDense();
31+
sparse.sum();
32+
sparse.mean();
33+
}
34+
const total = performance.now() - start;
35+
36+
console.log(
37+
JSON.stringify({
38+
function: "sparse_array",
39+
mean_ms: total / ITERATIONS,
40+
iterations: ITERATIONS,
41+
total_ms: total,
42+
}),
43+
);

0 commit comments

Comments
 (0)