Skip to content

Commit 880cbd3

Browse files
authored
Merge pull request #311 from githubnext/autoloop/perf-comparison
[Autoloop: perf-comparison] Iteration 315: add to_markdown / to_latex…
2 parents 02f7784 + b84f37d commit 880cbd3

4 files changed

Lines changed: 144 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Benchmark: str.findall, str.extract (first match), str.count on 10k-element string Series
3+
"""
4+
import json
5+
import time
6+
import pandas as pd
7+
8+
ROWS = 10_000
9+
WARMUP = 3
10+
ITERATIONS = 10
11+
12+
data = [f"item{i} code{i * 3} ref{i + 1}" for i in range(ROWS)]
13+
s = pd.Series(data)
14+
pat = r"\d+"
15+
16+
for _ in range(WARMUP):
17+
s.str.findall(pat)
18+
s.str.extract(r"(\d+)", expand=False)
19+
s.str.count(pat)
20+
21+
start = time.perf_counter()
22+
for _ in range(ITERATIONS):
23+
s.str.findall(pat)
24+
s.str.extract(r"(\d+)", expand=False)
25+
s.str.count(pat)
26+
total = (time.perf_counter() - start) * 1000
27+
28+
print(json.dumps({
29+
"function": "str_findall",
30+
"mean_ms": total / ITERATIONS,
31+
"iterations": ITERATIONS,
32+
"total_ms": total,
33+
}))
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""Benchmark: to_markdown and to_latex on a 1000-row DataFrame"""
2+
import json, time
3+
import numpy as np
4+
import pandas as pd
5+
6+
ROWS = 1_000
7+
WARMUP = 3
8+
ITERATIONS = 10
9+
10+
a = np.arange(ROWS) * 1.5
11+
b = [f"item_{i % 50}" for i in range(ROWS)]
12+
c = np.arange(ROWS) % 100
13+
df = pd.DataFrame({"a": a, "b": b, "c": c})
14+
15+
for _ in range(WARMUP):
16+
df.to_markdown()
17+
df.to_latex()
18+
19+
start_md = time.perf_counter()
20+
for _ in range(ITERATIONS):
21+
df.to_markdown()
22+
total_md = (time.perf_counter() - start_md) * 1000
23+
24+
start_ltx = time.perf_counter()
25+
for _ in range(ITERATIONS):
26+
df.to_latex()
27+
total_ltx = (time.perf_counter() - start_ltx) * 1000
28+
29+
total = total_md + total_ltx
30+
31+
print(json.dumps({
32+
"function": "to_markdown_latex",
33+
"mean_ms": total / (ITERATIONS * 2),
34+
"iterations": ITERATIONS * 2,
35+
"total_ms": total,
36+
}))
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Benchmark: strFindall, strFindFirst, strFindallCount on 10k-element string Series
3+
*/
4+
import { Series, strFindall, strFindFirst, strFindallCount } from "../../src/index.js";
5+
6+
const ROWS = 10_000;
7+
const WARMUP = 3;
8+
const ITERATIONS = 10;
9+
const data = Array.from({ length: ROWS }, (_, i) => `item${i} code${i * 3} ref${i + 1}`);
10+
const s = new Series({ data });
11+
const pat = /\d+/g;
12+
13+
for (let i = 0; i < WARMUP; i++) {
14+
strFindall(s, pat);
15+
strFindFirst(s, pat);
16+
strFindallCount(s, pat);
17+
}
18+
19+
const start = performance.now();
20+
for (let i = 0; i < ITERATIONS; i++) {
21+
strFindall(s, pat);
22+
strFindFirst(s, pat);
23+
strFindallCount(s, pat);
24+
}
25+
const total = performance.now() - start;
26+
27+
console.log(
28+
JSON.stringify({
29+
function: "str_findall",
30+
mean_ms: total / ITERATIONS,
31+
iterations: ITERATIONS,
32+
total_ms: total,
33+
}),
34+
);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Benchmark: toMarkdown and toLaTeX on a 1000-row DataFrame
3+
*/
4+
import { DataFrame, toMarkdown, toLaTeX } from "../../src/index.js";
5+
6+
const ROWS = 1_000;
7+
const WARMUP = 3;
8+
const ITERATIONS = 10;
9+
10+
const a = Float64Array.from({ length: ROWS }, (_, i) => i * 1.5);
11+
const b = Array.from({ length: ROWS }, (_, i) => `item_${i % 50}`);
12+
const c = Int32Array.from({ length: ROWS }, (_, i) => i % 100);
13+
const df = DataFrame.fromColumns({ a, b, c });
14+
15+
for (let i = 0; i < WARMUP; i++) {
16+
toMarkdown(df);
17+
toLaTeX(df);
18+
}
19+
20+
const startMd = performance.now();
21+
for (let i = 0; i < ITERATIONS; i++) {
22+
toMarkdown(df);
23+
}
24+
const totalMd = performance.now() - startMd;
25+
26+
const startLtx = performance.now();
27+
for (let i = 0; i < ITERATIONS; i++) {
28+
toLaTeX(df);
29+
}
30+
const totalLtx = performance.now() - startLtx;
31+
32+
const total = totalMd + totalLtx;
33+
34+
console.log(
35+
JSON.stringify({
36+
function: "to_markdown_latex",
37+
mean_ms: total / (ITERATIONS * 2),
38+
iterations: ITERATIONS * 2,
39+
total_ms: total,
40+
}),
41+
);

0 commit comments

Comments
 (0)