|
| 1 | +/** |
| 2 | + * Benchmark: crosstab() with normalize options — proportions by row/col/all. |
| 3 | + * Outputs JSON: {"function": "crosstab_normalize", "mean_ms": ..., "iterations": ..., "total_ms": ...} |
| 4 | + */ |
| 5 | +import { Series, crosstab } from "../../src/index.ts"; |
| 6 | + |
| 7 | +const SIZE = 50_000; |
| 8 | +const WARMUP = 5; |
| 9 | +const ITERATIONS = 30; |
| 10 | + |
| 11 | +let seed = 99; |
| 12 | +const rand = () => { |
| 13 | + seed = (seed * 1664525 + 1013904223) & 0x7fffffff; |
| 14 | + return seed; |
| 15 | +}; |
| 16 | + |
| 17 | +const choices_a = ["north", "south", "east", "west"]; |
| 18 | +const choices_b = ["red", "green", "blue"]; |
| 19 | + |
| 20 | +const a = new Series({ data: Array.from({ length: SIZE }, () => choices_a[rand() % 4]) }); |
| 21 | +const b = new Series({ data: Array.from({ length: SIZE }, () => choices_b[rand() % 3]) }); |
| 22 | + |
| 23 | +for (let i = 0; i < WARMUP; i++) { |
| 24 | + crosstab(a, b, { normalize: true }); |
| 25 | + crosstab(a, b, { normalize: "index" }); |
| 26 | + crosstab(a, b, { normalize: "columns" }); |
| 27 | +} |
| 28 | + |
| 29 | +const times: number[] = []; |
| 30 | +for (let i = 0; i < ITERATIONS; i++) { |
| 31 | + const t0 = performance.now(); |
| 32 | + crosstab(a, b, { normalize: true }); |
| 33 | + crosstab(a, b, { normalize: "index" }); |
| 34 | + crosstab(a, b, { normalize: "columns" }); |
| 35 | + times.push(performance.now() - t0); |
| 36 | +} |
| 37 | + |
| 38 | +const total_ms = times.reduce((a, b) => a + b, 0); |
| 39 | +console.log( |
| 40 | + JSON.stringify({ |
| 41 | + function: "crosstab_normalize", |
| 42 | + mean_ms: Math.round((total_ms / ITERATIONS) * 1000) / 1000, |
| 43 | + iterations: ITERATIONS, |
| 44 | + total_ms: Math.round(total_ms * 1000) / 1000, |
| 45 | + }), |
| 46 | +); |
0 commit comments