Skip to content

Commit 3b519cd

Browse files
[Autoloop: perf-comparison] Iteration 409: readStata/toStata benchmark
Add TypeScript (readStata from in-memory DTA buffer) and Python (pd.read_stata from temp file) benchmarks for 10k-row DataFrame with int32/float64/string columns, 20 measured iterations. Run: https://github.com/githubnext/tsb/actions/runs/29668492326 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 4544000 commit 3b519cd

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Benchmark: readStata / toStata round-trip on a 10k-row DataFrame"""
2+
import json, time, tempfile, os
3+
import numpy as np
4+
import pandas as pd
5+
6+
ROWS = 10_000
7+
WARMUP = 3
8+
ITERATIONS = 20
9+
10+
ids = np.arange(ROWS, dtype=np.int32)
11+
values = np.sin(np.arange(ROWS) * 0.01) * 1000
12+
categories = np.array([f"cat_{i % 5}" for i in range(ROWS)])
13+
14+
df = pd.DataFrame({"id": ids, "value": values, "category": categories})
15+
16+
# Write to a temp Stata file so read_stata benchmarks read from disk
17+
tmp = tempfile.NamedTemporaryFile(suffix=".dta", delete=False)
18+
tmp.close()
19+
df.to_stata(tmp.name, write_index=False)
20+
21+
# Warm up
22+
for _ in range(WARMUP):
23+
pd.read_stata(tmp.name)
24+
25+
start = time.perf_counter()
26+
for _ in range(ITERATIONS):
27+
pd.read_stata(tmp.name)
28+
total = (time.perf_counter() - start) * 1000
29+
30+
os.unlink(tmp.name)
31+
32+
print(json.dumps({
33+
"function": "readStata",
34+
"mean_ms": total / ITERATIONS,
35+
"iterations": ITERATIONS,
36+
"total_ms": total,
37+
}))

benchmarks/tsb/bench_readStata.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Benchmark: readStata / toStata round-trip on a 10k-row DataFrame
3+
*/
4+
import { DataFrame, Series, readStata, toStata } from "../../src/index.js";
5+
6+
const ROWS = 10_000;
7+
const WARMUP = 3;
8+
const ITERATIONS = 20;
9+
10+
// Build a DataFrame with numeric and string columns
11+
const ids = Int32Array.from({ length: ROWS }, (_, i) => i);
12+
const values = Float64Array.from({ length: ROWS }, (_, i) => Math.sin(i * 0.01) * 1000);
13+
const categories = Array.from({ length: ROWS }, (_, i) => `cat_${i % 5}`);
14+
15+
const df = new DataFrame({
16+
id: new Series(ids),
17+
value: new Series(values),
18+
category: new Series(categories),
19+
});
20+
21+
// Serialize once so readStata benchmarks read from a pre-built buffer
22+
const buf = toStata(df);
23+
24+
// Warm up
25+
for (let i = 0; i < WARMUP; i++) {
26+
readStata(buf);
27+
}
28+
29+
const start = performance.now();
30+
for (let i = 0; i < ITERATIONS; i++) {
31+
readStata(buf);
32+
}
33+
const total = performance.now() - start;
34+
35+
console.log(
36+
JSON.stringify({
37+
function: "readStata",
38+
mean_ms: total / ITERATIONS,
39+
iterations: ITERATIONS,
40+
total_ms: total,
41+
}),
42+
);

0 commit comments

Comments
 (0)