Skip to content

Commit 7505d03

Browse files
[Autoloop: perf-comparison] Iteration 408: add readFwf benchmark pair
Run: https://github.com/githubnext/tsb/actions/runs/29645946373 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 185e250 commit 7505d03

2 files changed

Lines changed: 76 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: read_fwf — parse a fixed-width formatted text file into a DataFrame.
2+
Dataset: 10,000 rows x 4 columns (id, name, value, flag).
3+
"""
4+
import json, time, io
5+
import numpy as np
6+
import pandas as pd
7+
8+
ROWS = 10_000
9+
WARMUP = 3
10+
ITERATIONS = 10
11+
12+
# Build fixed-width text matching the TypeScript benchmark
13+
lines = ["id name value flag"]
14+
for i in range(ROWS):
15+
id_col = str(i).rjust(6)
16+
name_col = ("item" + str(i % 500)).ljust(10)
17+
value_col = f"{np.sin(i * 0.01) * 1000:.3f}".rjust(10)
18+
flag_col = ("Y" if i % 2 == 0 else "N").ljust(4)
19+
lines.append(id_col + name_col + value_col + flag_col)
20+
text = "\n".join(lines)
21+
22+
colspecs = [(0, 6), (6, 16), (16, 26), (26, 30)]
23+
24+
for _ in range(WARMUP):
25+
pd.read_fwf(io.StringIO(text), colspecs=colspecs, header=0)
26+
27+
start = time.perf_counter()
28+
for _ in range(ITERATIONS):
29+
pd.read_fwf(io.StringIO(text), colspecs=colspecs, header=0)
30+
total = (time.perf_counter() - start) * 1000
31+
32+
print(json.dumps({
33+
"function": "readFwf",
34+
"mean_ms": total / ITERATIONS,
35+
"iterations": ITERATIONS,
36+
"total_ms": total,
37+
}))

benchmarks/tsb/bench_read_fwf.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Benchmark: readFwf — parse a fixed-width formatted text file into a DataFrame.
3+
* Dataset: 10,000 rows × 4 columns (id, name, value, flag).
4+
*/
5+
import { readFwf } from "../../src/index.js";
6+
7+
const ROWS = 10_000;
8+
const WARMUP = 3;
9+
const ITERATIONS = 10;
10+
11+
// Build a fixed-width text: id(6), name(10), value(10), flag(4)
12+
const lines: string[] = ["id name value flag"];
13+
for (let i = 0; i < ROWS; i++) {
14+
const id = String(i).padStart(6);
15+
const name = ("item" + (i % 500)).padEnd(10);
16+
const value = (Math.sin(i * 0.01) * 1000).toFixed(3).padStart(10);
17+
const flag = (i % 2 === 0 ? "Y" : "N").padEnd(4);
18+
lines.push(id + name + value + flag);
19+
}
20+
const text = lines.join("\n");
21+
22+
for (let i = 0; i < WARMUP; i++) {
23+
readFwf(text, { colspecs: [[0, 6], [6, 16], [16, 26], [26, 30]] });
24+
}
25+
26+
const start = performance.now();
27+
for (let i = 0; i < ITERATIONS; i++) {
28+
readFwf(text, { colspecs: [[0, 6], [6, 16], [16, 26], [26, 30]] });
29+
}
30+
const total = performance.now() - start;
31+
32+
console.log(
33+
JSON.stringify({
34+
function: "readFwf",
35+
mean_ms: total / ITERATIONS,
36+
iterations: ITERATIONS,
37+
total_ms: total,
38+
}),
39+
);

0 commit comments

Comments
 (0)