Skip to content

Commit a0d31b4

Browse files
Iteration 116: Add 5 benchmark pairs (345 total, +5 vs best 340)
Run: https://github.com/githubnext/tsessebe/actions/runs/24484182038 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 8bbcb23 commit a0d31b4

10 files changed

Lines changed: 229 additions & 0 deletions
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""Benchmark: pd.Categorical.from_codes on 100k-element code array"""
2+
import json, time
3+
import pandas as pd
4+
5+
ROWS = 100_000
6+
WARMUP = 3
7+
ITERATIONS = 10
8+
categories = ["alpha", "beta", "gamma", "delta"]
9+
codes = [i % 4 for i in range(ROWS)]
10+
11+
for _ in range(WARMUP):
12+
pd.Categorical.from_codes(codes, categories=categories)
13+
14+
start = time.perf_counter()
15+
for _ in range(ITERATIONS):
16+
pd.Categorical.from_codes(codes, categories=categories)
17+
total = (time.perf_counter() - start) * 1000
18+
print(json.dumps({"function": "cat_from_codes", "mean_ms": total / ITERATIONS, "iterations": ITERATIONS, "total_ms": total}))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""Benchmark: MultiIndex.isin() on 100k-pair MultiIndex"""
2+
import json, time
3+
import pandas as pd
4+
5+
ROWS = 100_000
6+
WARMUP = 3
7+
ITERATIONS = 10
8+
a = [f"a{i % 100}" for i in range(ROWS)]
9+
b = [i % 1000 for i in range(ROWS)]
10+
tuples = list(zip(a, b))
11+
mi = pd.MultiIndex.from_tuples(tuples)
12+
lookup_tuples = [(f"a{i % 100}", i % 1000) for i in range(1000)]
13+
14+
for _ in range(WARMUP):
15+
mi.isin(lookup_tuples)
16+
17+
start = time.perf_counter()
18+
for _ in range(ITERATIONS):
19+
mi.isin(lookup_tuples)
20+
total = (time.perf_counter() - start) * 1000
21+
print(json.dumps({"function": "multi_index_isin", "mean_ms": total / ITERATIONS, "iterations": ITERATIONS, "total_ms": total}))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""Benchmark: MultiIndex.reorder_levels() on 100k-pair MultiIndex"""
2+
import json, time
3+
import pandas as pd
4+
5+
ROWS = 100_000
6+
WARMUP = 3
7+
ITERATIONS = 10
8+
a = [f"a{i % 100}" for i in range(ROWS)]
9+
b = [i % 1000 for i in range(ROWS)]
10+
c = [i % 50 for i in range(ROWS)]
11+
tuples = list(zip(a, b, c))
12+
mi = pd.MultiIndex.from_tuples(tuples)
13+
14+
for _ in range(WARMUP):
15+
mi.reorder_levels([2, 0, 1])
16+
17+
start = time.perf_counter()
18+
for _ in range(ITERATIONS):
19+
mi.reorder_levels([2, 0, 1])
20+
total = (time.perf_counter() - start) * 1000
21+
print(json.dumps({"function": "multi_index_reorder_levels", "mean_ms": total / ITERATIONS, "iterations": ITERATIONS, "total_ms": total}))
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""Benchmark: MultiIndex.set_names() on 100k-pair MultiIndex"""
2+
import json, time
3+
import pandas as pd
4+
5+
ROWS = 100_000
6+
WARMUP = 3
7+
ITERATIONS = 10
8+
a = [f"a{i % 100}" for i in range(ROWS)]
9+
b = [i % 1000 for i in range(ROWS)]
10+
tuples = list(zip(a, b))
11+
mi = pd.MultiIndex.from_tuples(tuples)
12+
13+
for _ in range(WARMUP):
14+
mi.set_names(["level0", "level1"])
15+
16+
start = time.perf_counter()
17+
for _ in range(ITERATIONS):
18+
mi.set_names(["level0", "level1"])
19+
total = (time.perf_counter() - start) * 1000
20+
print(json.dumps({"function": "multi_index_set_names", "mean_ms": total / ITERATIONS, "iterations": ITERATIONS, "total_ms": total}))
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""Benchmark: MultiIndex.to_flat_index() (equivalent of toArray()) on 100k-pair MultiIndex"""
2+
import json, time
3+
import pandas as pd
4+
5+
ROWS = 100_000
6+
WARMUP = 3
7+
ITERATIONS = 10
8+
a = [f"a{i % 100}" for i in range(ROWS)]
9+
b = [i % 1000 for i in range(ROWS)]
10+
tuples = list(zip(a, b))
11+
mi = pd.MultiIndex.from_tuples(tuples)
12+
13+
for _ in range(WARMUP):
14+
mi.to_flat_index()
15+
16+
start = time.perf_counter()
17+
for _ in range(ITERATIONS):
18+
mi.to_flat_index()
19+
total = (time.perf_counter() - start) * 1000
20+
print(json.dumps({"function": "multi_index_to_array", "mean_ms": total / ITERATIONS, "iterations": ITERATIONS, "total_ms": total}))
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Benchmark: catFromCodes on 100k-element code array
3+
*/
4+
import { catFromCodes } from "../../src/index.js";
5+
6+
const ROWS = 100_000;
7+
const WARMUP = 3;
8+
const ITERATIONS = 10;
9+
const categories = ["alpha", "beta", "gamma", "delta"];
10+
const codes = Array.from({ length: ROWS }, (_, i) => i % 4);
11+
12+
for (let i = 0; i < WARMUP; i++) catFromCodes(codes, categories);
13+
const start = performance.now();
14+
for (let i = 0; i < ITERATIONS; i++) catFromCodes(codes, categories);
15+
const total = performance.now() - start;
16+
console.log(
17+
JSON.stringify({
18+
function: "cat_from_codes",
19+
mean_ms: total / ITERATIONS,
20+
iterations: ITERATIONS,
21+
total_ms: total,
22+
}),
23+
);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Benchmark: MultiIndex.isin() on 100k-pair MultiIndex
3+
*/
4+
import { MultiIndex } from "../../src/index.js";
5+
6+
const ROWS = 100_000;
7+
const WARMUP = 3;
8+
const ITERATIONS = 10;
9+
const a = Array.from({ length: ROWS }, (_, i) => `a${i % 100}`);
10+
const b = Array.from({ length: ROWS }, (_, i) => i % 1000);
11+
const tuples: [string, number][] = a.map((v, i) => [v, b[i] as number]);
12+
const mi = new MultiIndex({ tuples });
13+
// 1000 tuples to look up
14+
const lookupTuples: [string, number][] = Array.from({ length: 1000 }, (_, i) => [
15+
`a${i % 100}`,
16+
i % 1000,
17+
]);
18+
19+
for (let i = 0; i < WARMUP; i++) mi.isin(lookupTuples);
20+
const start = performance.now();
21+
for (let i = 0; i < ITERATIONS; i++) mi.isin(lookupTuples);
22+
const total = performance.now() - start;
23+
console.log(
24+
JSON.stringify({
25+
function: "multi_index_isin",
26+
mean_ms: total / ITERATIONS,
27+
iterations: ITERATIONS,
28+
total_ms: total,
29+
}),
30+
);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Benchmark: MultiIndex.reorderLevels() on 100k-pair MultiIndex
3+
*/
4+
import { MultiIndex } from "../../src/index.js";
5+
6+
const ROWS = 100_000;
7+
const WARMUP = 3;
8+
const ITERATIONS = 10;
9+
const a = Array.from({ length: ROWS }, (_, i) => `a${i % 100}`);
10+
const b = Array.from({ length: ROWS }, (_, i) => i % 1000);
11+
const c = Array.from({ length: ROWS }, (_, i) => i % 50);
12+
const tuples: [string, number, number][] = a.map((v, i) => [v, b[i] as number, c[i] as number]);
13+
const mi = new MultiIndex({ tuples });
14+
15+
for (let i = 0; i < WARMUP; i++) mi.reorderLevels([2, 0, 1]);
16+
const start = performance.now();
17+
for (let i = 0; i < ITERATIONS; i++) mi.reorderLevels([2, 0, 1]);
18+
const total = performance.now() - start;
19+
console.log(
20+
JSON.stringify({
21+
function: "multi_index_reorder_levels",
22+
mean_ms: total / ITERATIONS,
23+
iterations: ITERATIONS,
24+
total_ms: total,
25+
}),
26+
);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Benchmark: MultiIndex.setNames() on 100k-pair MultiIndex
3+
*/
4+
import { MultiIndex } from "../../src/index.js";
5+
6+
const ROWS = 100_000;
7+
const WARMUP = 3;
8+
const ITERATIONS = 10;
9+
const a = Array.from({ length: ROWS }, (_, i) => `a${i % 100}`);
10+
const b = Array.from({ length: ROWS }, (_, i) => i % 1000);
11+
const tuples: [string, number][] = a.map((v, i) => [v, b[i] as number]);
12+
const mi = new MultiIndex({ tuples });
13+
14+
for (let i = 0; i < WARMUP; i++) mi.setNames(["level0", "level1"]);
15+
const start = performance.now();
16+
for (let i = 0; i < ITERATIONS; i++) mi.setNames(["level0", "level1"]);
17+
const total = performance.now() - start;
18+
console.log(
19+
JSON.stringify({
20+
function: "multi_index_set_names",
21+
mean_ms: total / ITERATIONS,
22+
iterations: ITERATIONS,
23+
total_ms: total,
24+
}),
25+
);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Benchmark: MultiIndex.toArray() on 100k-pair MultiIndex
3+
*/
4+
import { MultiIndex } from "../../src/index.js";
5+
6+
const ROWS = 100_000;
7+
const WARMUP = 3;
8+
const ITERATIONS = 10;
9+
const a = Array.from({ length: ROWS }, (_, i) => `a${i % 100}`);
10+
const b = Array.from({ length: ROWS }, (_, i) => i % 1000);
11+
const tuples: [string, number][] = a.map((v, i) => [v, b[i] as number]);
12+
const mi = new MultiIndex({ tuples });
13+
14+
for (let i = 0; i < WARMUP; i++) mi.toArray();
15+
const start = performance.now();
16+
for (let i = 0; i < ITERATIONS; i++) mi.toArray();
17+
const total = performance.now() - start;
18+
console.log(
19+
JSON.stringify({
20+
function: "multi_index_to_array",
21+
mean_ms: total / ITERATIONS,
22+
iterations: ITERATIONS,
23+
total_ms: total,
24+
}),
25+
);

0 commit comments

Comments
 (0)