Skip to content

Commit 9c0ba4c

Browse files
[Autoloop: perf-comparison] Iteration 375: Add 3 resample benchmark pairs (ohlc, first_last, std_var_size)
Run: https://github.com/githubnext/tsb/actions/runs/28260214481 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent e1f37a3 commit 9c0ba4c

6 files changed

Lines changed: 214 additions & 0 deletions
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""Benchmark: resample_first_last — pd.Series.resample("H").first() / .last()."""
2+
import time
3+
import pandas as pd
4+
import numpy as np
5+
6+
SIZE = 50_000
7+
WARMUP = 3
8+
ITERATIONS = 30
9+
10+
base = pd.Timestamp("2020-01-01T00:00:00Z")
11+
idx = pd.date_range(start=base, periods=SIZE, freq="min")
12+
data = [(i % 100) * 2.5 + np.cos(i * 0.01) * 10 for i in range(SIZE)]
13+
14+
s = pd.Series(data, index=idx)
15+
16+
for _ in range(WARMUP):
17+
s.resample("H").first()
18+
s.resample("H").last()
19+
20+
times = []
21+
for _ in range(ITERATIONS):
22+
t0 = time.perf_counter()
23+
s.resample("H").first()
24+
s.resample("H").last()
25+
times.append(time.perf_counter() - t0)
26+
27+
total = sum(times)
28+
mean_ms = (total / ITERATIONS) * 1000
29+
total_ms = total * 1000
30+
print(f'{{"function": "resample_first_last", "mean_ms": {mean_ms:.6f}, "iterations": {ITERATIONS}, "total_ms": {total_ms:.6f}}}')
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""Benchmark: resample_ohlc — pd.Series.resample("H").ohlc() — OHLC aggregation."""
2+
import time
3+
import pandas as pd
4+
import numpy as np
5+
6+
SIZE = 50_000
7+
WARMUP = 3
8+
ITERATIONS = 30
9+
10+
base = pd.Timestamp("2020-01-01T00:00:00Z")
11+
idx = pd.date_range(start=base, periods=SIZE, freq="min")
12+
data = [np.sin(i * 0.03) * 100 + 200 for i in range(SIZE)]
13+
14+
s = pd.Series(data, index=idx)
15+
16+
for _ in range(WARMUP):
17+
s.resample("H").ohlc()
18+
19+
times = []
20+
for _ in range(ITERATIONS):
21+
t0 = time.perf_counter()
22+
s.resample("H").ohlc()
23+
times.append(time.perf_counter() - t0)
24+
25+
total = sum(times)
26+
mean_ms = (total / ITERATIONS) * 1000
27+
total_ms = total * 1000
28+
print(f'{{"function": "resample_ohlc", "mean_ms": {mean_ms:.6f}, "iterations": {ITERATIONS}, "total_ms": {total_ms:.6f}}}')
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""Benchmark: resample_std_var_size — pd.Series.resample("H").std() / .var() / .size()."""
2+
import time
3+
import pandas as pd
4+
import numpy as np
5+
6+
SIZE = 50_000
7+
WARMUP = 3
8+
ITERATIONS = 30
9+
10+
base = pd.Timestamp("2020-01-01T00:00:00Z")
11+
idx = pd.date_range(start=base, periods=SIZE, freq="min")
12+
data = [np.sin(i * 0.05) * 50 + (i % 60) * 0.5 for i in range(SIZE)]
13+
14+
s = pd.Series(data, index=idx)
15+
16+
for _ in range(WARMUP):
17+
s.resample("H").std()
18+
s.resample("H").var()
19+
s.resample("H").size()
20+
21+
times = []
22+
for _ in range(ITERATIONS):
23+
t0 = time.perf_counter()
24+
s.resample("H").std()
25+
s.resample("H").var()
26+
s.resample("H").size()
27+
times.append(time.perf_counter() - t0)
28+
29+
total = sum(times)
30+
mean_ms = (total / ITERATIONS) * 1000
31+
total_ms = total * 1000
32+
print(f'{{"function": "resample_std_var_size", "mean_ms": {mean_ms:.6f}, "iterations": {ITERATIONS}, "total_ms": {total_ms:.6f}}}')
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Benchmark: resample_first_last — SeriesResampler.first() and .last() on hourly resampling.
3+
*
4+
* Mirrors pandas: pd.Series.resample("H").first() / .last()
5+
* first() returns the first non-null value per bin; last() returns the last.
6+
*
7+
* Outputs JSON: {"function": "resample_first_last", "mean_ms": ..., "iterations": ..., "total_ms": ...}
8+
*/
9+
import { Series, resampleSeries } from "../../src/index.ts";
10+
11+
const SIZE = 50_000;
12+
const WARMUP = 3;
13+
const ITERATIONS = 30;
14+
15+
const base = new Date("2020-01-01T00:00:00Z").getTime();
16+
const idx = Array.from({ length: SIZE }, (_, i) => new Date(base + i * 60_000));
17+
const data = Array.from({ length: SIZE }, (_, i) => (i % 100) * 2.5 + Math.cos(i * 0.01) * 10);
18+
19+
const s = new Series({ data, index: idx });
20+
21+
for (let i = 0; i < WARMUP; i++) {
22+
resampleSeries(s, "H").first();
23+
resampleSeries(s, "H").last();
24+
}
25+
26+
const times: number[] = [];
27+
for (let i = 0; i < ITERATIONS; i++) {
28+
const t0 = performance.now();
29+
resampleSeries(s, "H").first();
30+
resampleSeries(s, "H").last();
31+
times.push(performance.now() - t0);
32+
}
33+
const total = times.reduce((a, b) => a + b, 0);
34+
console.log(
35+
JSON.stringify({
36+
function: "resample_first_last",
37+
mean_ms: total / ITERATIONS,
38+
iterations: ITERATIONS,
39+
total_ms: total,
40+
}),
41+
);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Benchmark: resample_ohlc — SeriesResampler.ohlc() — OHLC aggregation on daily resampling.
3+
*
4+
* Mirrors pandas: pd.Series.resample("D").ohlc()
5+
* ohlc() returns a DataFrame with open/high/low/close columns, one row per time bin.
6+
*
7+
* Outputs JSON: {"function": "resample_ohlc", "mean_ms": ..., "iterations": ..., "total_ms": ...}
8+
*/
9+
import { Series, resampleSeries } from "../../src/index.ts";
10+
11+
const SIZE = 50_000;
12+
const WARMUP = 3;
13+
const ITERATIONS = 30;
14+
15+
const base = new Date("2020-01-01T00:00:00Z").getTime();
16+
const idx = Array.from({ length: SIZE }, (_, i) => new Date(base + i * 60_000));
17+
const data = Array.from({ length: SIZE }, (_, i) => Math.sin(i * 0.03) * 100 + 200);
18+
19+
const s = new Series({ data, index: idx });
20+
21+
for (let i = 0; i < WARMUP; i++) {
22+
resampleSeries(s, "H").ohlc();
23+
}
24+
25+
const times: number[] = [];
26+
for (let i = 0; i < ITERATIONS; i++) {
27+
const t0 = performance.now();
28+
resampleSeries(s, "H").ohlc();
29+
times.push(performance.now() - t0);
30+
}
31+
const total = times.reduce((a, b) => a + b, 0);
32+
console.log(
33+
JSON.stringify({
34+
function: "resample_ohlc",
35+
mean_ms: total / ITERATIONS,
36+
iterations: ITERATIONS,
37+
total_ms: total,
38+
}),
39+
);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Benchmark: resample_std_var_size — SeriesResampler.std(), .var(), .size() on hourly bins.
3+
*
4+
* Mirrors pandas: pd.Series.resample("H").std() / .var() / .size()
5+
* std() computes standard deviation per bin, var() computes variance,
6+
* size() returns the count per bin.
7+
*
8+
* Outputs JSON: {"function": "resample_std_var_size", "mean_ms": ..., "iterations": ..., "total_ms": ...}
9+
*/
10+
import { Series, resampleSeries } from "../../src/index.ts";
11+
12+
const SIZE = 50_000;
13+
const WARMUP = 3;
14+
const ITERATIONS = 30;
15+
16+
const base = new Date("2020-01-01T00:00:00Z").getTime();
17+
const idx = Array.from({ length: SIZE }, (_, i) => new Date(base + i * 60_000));
18+
const data = Array.from({ length: SIZE }, (_, i) => Math.sin(i * 0.05) * 50 + (i % 60) * 0.5);
19+
20+
const s = new Series({ data, index: idx });
21+
22+
for (let i = 0; i < WARMUP; i++) {
23+
resampleSeries(s, "H").std();
24+
resampleSeries(s, "H").var();
25+
resampleSeries(s, "H").size();
26+
}
27+
28+
const times: number[] = [];
29+
for (let i = 0; i < ITERATIONS; i++) {
30+
const t0 = performance.now();
31+
resampleSeries(s, "H").std();
32+
resampleSeries(s, "H").var();
33+
resampleSeries(s, "H").size();
34+
times.push(performance.now() - t0);
35+
}
36+
const total = times.reduce((a, b) => a + b, 0);
37+
console.log(
38+
JSON.stringify({
39+
function: "resample_std_var_size",
40+
mean_ms: total / ITERATIONS,
41+
iterations: ITERATIONS,
42+
total_ms: total,
43+
}),
44+
);

0 commit comments

Comments
 (0)