Skip to content

Commit 2073b71

Browse files
Iteration 192: 2 new benchmark pairs (natsort, insert_pop) — 534→536
Run: https://github.com/githubnext/tsessebe/actions/runs/24607061335 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 46714bd commit 2073b71

4 files changed

Lines changed: 159 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: DataFrame.insert() and DataFrame.pop() on a 10k-row DataFrame.
2+
3+
Mirrors tsb's insertColumn, popColumn, reorderColumns, moveColumn benchmarks.
4+
"""
5+
import json, time
6+
import numpy as np
7+
import pandas as pd
8+
9+
ROWS = 10_000
10+
WARMUP = 3
11+
ITERATIONS = 10
12+
13+
data = np.arange(ROWS)
14+
df = pd.DataFrame({"a": data, "b": data, "c": data, "d": data})
15+
extra_col = data * 2
16+
17+
def run():
18+
df2 = df.copy()
19+
df2.insert(2, "x", extra_col)
20+
df2.pop("x")
21+
df[["d", "c", "b", "a"]] # reorderColumns
22+
df[["c", "a", "b", "d"]] # moveColumn equivalent
23+
24+
for _ in range(WARMUP):
25+
run()
26+
27+
start = time.perf_counter()
28+
for _ in range(ITERATIONS):
29+
run()
30+
total = (time.perf_counter() - start) * 1000
31+
32+
print(json.dumps({
33+
"function": "insert_pop",
34+
"mean_ms": total / ITERATIONS,
35+
"iterations": ITERATIONS,
36+
"total_ms": total,
37+
}))

benchmarks/pandas/bench_natsort.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""Benchmark: natsort — natural-order sorting of 10k strings with numeric suffixes.
2+
3+
Mirrors tsb's natSorted / natCompare / natSortKey / natArgSort using the
4+
Python `natsort` package (falls back to a manual key if natsort not installed).
5+
"""
6+
import json, time
7+
8+
N = 10_000
9+
WARMUP = 3
10+
ITERATIONS = 10
11+
12+
# Build the same dataset as the TS benchmark
13+
items = [f"item{N - i}" for i in range(N)]
14+
15+
try:
16+
from natsort import natsorted, natsort_keygen
17+
nat_key = natsort_keygen()
18+
def run():
19+
natsorted(items)
20+
nat_key("file42")
21+
except ImportError:
22+
# Fallback: manual digit-aware key (equivalent logic)
23+
import re
24+
def _nat_key(s):
25+
return [int(t) if t.isdigit() else t for t in re.split(r"(\d+)", s)]
26+
def run():
27+
sorted(items, key=_nat_key)
28+
_nat_key("file42")
29+
30+
for _ in range(WARMUP):
31+
run()
32+
33+
start = time.perf_counter()
34+
for _ in range(ITERATIONS):
35+
run()
36+
total = (time.perf_counter() - start) * 1000
37+
38+
print(json.dumps({
39+
"function": "natsort",
40+
"mean_ms": total / ITERATIONS,
41+
"iterations": ITERATIONS,
42+
"total_ms": total,
43+
}))

benchmarks/tsb/bench_insert_pop.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Benchmark: insertColumn, popColumn, reorderColumns, moveColumn on a 10k-row DataFrame
3+
*
4+
* Mirrors pandas DataFrame.insert() and DataFrame.pop() operations.
5+
*/
6+
import { DataFrame, insertColumn, popColumn, reorderColumns, moveColumn } from "../../src/index.js";
7+
8+
const ROWS = 10_000;
9+
const WARMUP = 3;
10+
const ITERATIONS = 10;
11+
12+
const data = Array.from({ length: ROWS }, (_, i) => i);
13+
const df = DataFrame.fromColumns({ a: data, b: data, c: data, d: data });
14+
const extraCol = Array.from({ length: ROWS }, (_, i) => i * 2);
15+
16+
for (let i = 0; i < WARMUP; i++) {
17+
const df2 = insertColumn(df, 2, "x", extraCol);
18+
popColumn(df2, "x");
19+
reorderColumns(df, ["d", "c", "b", "a"]);
20+
moveColumn(df, "c", 0);
21+
}
22+
23+
const start = performance.now();
24+
for (let i = 0; i < ITERATIONS; i++) {
25+
const df2 = insertColumn(df, 2, "x", extraCol);
26+
popColumn(df2, "x");
27+
reorderColumns(df, ["d", "c", "b", "a"]);
28+
moveColumn(df, "c", 0);
29+
}
30+
const total = performance.now() - start;
31+
32+
console.log(
33+
JSON.stringify({
34+
function: "insert_pop",
35+
mean_ms: total / ITERATIONS,
36+
iterations: ITERATIONS,
37+
total_ms: total,
38+
}),
39+
);

benchmarks/tsb/bench_natsort.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Benchmark: natSorted, natCompare, natSortKey, natArgSort on 10k strings
3+
*
4+
* Mirrors Python `natsort` package usage: natural-order sorting of strings
5+
* with embedded numeric tokens (e.g. "file10" sorts after "file9").
6+
*/
7+
import { natSorted, natCompare, natSortKey, natArgSort } from "../../src/index.js";
8+
9+
const N = 10_000;
10+
const WARMUP = 3;
11+
const ITERATIONS = 10;
12+
13+
// Build an array of strings with numeric suffixes (out of natural order)
14+
const items = Array.from({ length: N }, (_, i) => `item${N - i}`);
15+
16+
// Warm-up
17+
for (let i = 0; i < WARMUP; i++) {
18+
natSorted(items);
19+
natCompare("file10", "file9");
20+
natSortKey("file42");
21+
natArgSort(items);
22+
}
23+
24+
const start = performance.now();
25+
for (let i = 0; i < ITERATIONS; i++) {
26+
natSorted(items);
27+
natCompare("file10", "file9");
28+
natSortKey("file42");
29+
natArgSort(items);
30+
}
31+
const total = performance.now() - start;
32+
33+
console.log(
34+
JSON.stringify({
35+
function: "natsort",
36+
mean_ms: total / ITERATIONS,
37+
iterations: ITERATIONS,
38+
total_ms: total,
39+
}),
40+
);

0 commit comments

Comments
 (0)