Skip to content

Commit f101f9d

Browse files
committed
chore: update benchmark
1 parent e287ca1 commit f101f9d

File tree

1 file changed

+49
-24
lines changed

1 file changed

+49
-24
lines changed

benchmark/combinationOfCardinalityAndSize.js

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,39 @@
11
import { writeFile } from 'node:fs/promises';
22
import path from 'node:path';
33

4-
import { bench, do_not_optimize, lineplot, run } from 'mitata';
4+
import { run, bench, do_not_optimize, lineplot } from 'mitata';
5+
import { Matrix } from 'ml-matrix';
56
import { xSequentialFillFromStep } from 'ml-spectra-processing';
67

78
import { SparseMatrix } from '../src/index.js';
89

910
import { randomMatrix } from './utils/randomMatrix.js';
10-
11+
import { mmulMediumDensity } from '../src/utils/mmulMediumDensity.js';
12+
import { mmulSmall } from '../src/utils/mmulSmall.js';
13+
import { mmulLowDensity } from '../src/utils/mmulLowDensity.js';
14+
15+
/* eslint
16+
func-names: 0
17+
camelcase: 0
18+
*/
1119
// Prepare matrices once
1220
const cardinalities = Array.from(
13-
xSequentialFillFromStep({ from: 10, step: 5, size: 2 }),
21+
xSequentialFillFromStep({ from: 10, step: 50, size: 9 }),
1422
);
1523

1624
// const dimensions = Array.from(
17-
// xSequentialFillFromStep({ from: 700, step: 100, size: 13 }),
25+
// xSequentialFillFromStep({ from: 30, step: 100, size: 20 }),
1826
// );
1927

20-
const dimensions = [512];
28+
const dimensions = [32];
2129
lineplot(() => {
2230
bench('hibrid($cardinality,$dimension)', function* (ctx) {
2331
const cardinality = ctx.get('cardinality');
2432
const size = ctx.get('dimension');
2533
// Prepare matrices once
2634
let A = new SparseMatrix(randomMatrix(size, size, cardinality));
2735
let B = new SparseMatrix(randomMatrix(size, size, cardinality));
28-
36+
A.mmul(B);
2937
// Benchmark the multiplication
3038
yield () => do_not_optimize(A.mmul(B));
3139
do_not_optimize(A);
@@ -41,9 +49,9 @@ lineplot(() => {
4149
// Prepare matrices once
4250
let A = new SparseMatrix(randomMatrix(size, size, cardinality));
4351
let B = new SparseMatrix(randomMatrix(size, size, cardinality));
44-
52+
mmulSmall(A, B);
4553
// Benchmark the multiplication
46-
yield () => do_not_optimize(A._mmulSmall(B));
54+
yield () => do_not_optimize(mmulSmall(A, B));
4755
// Explicit cleanup
4856
do_not_optimize(A);
4957
do_not_optimize(B);
@@ -58,9 +66,26 @@ lineplot(() => {
5866
// Prepare matrices once
5967
let A = new SparseMatrix(randomMatrix(size, size, cardinality));
6068
let B = new SparseMatrix(randomMatrix(size, size, cardinality));
69+
mmulLowDensity(A, B);
70+
// Benchmark the multiplication
71+
yield () => do_not_optimize(mmulLowDensity(A, B));
72+
// Explicit cleanup
73+
do_not_optimize(A);
74+
do_not_optimize(B);
75+
})
76+
.gc('inner')
77+
.args('cardinality', cardinalities) //.range('size', 32, 1024, 2); //.args('size', sizes);
78+
.args('dimension', dimensions);
6179

80+
bench('dense($cardinality,$dimension)', function* (ctx) {
81+
const cardinality = ctx.get('cardinality');
82+
const size = ctx.get('dimension');
83+
// Prepare matrices once
84+
let A = new Matrix(randomMatrix(size, size, cardinality));
85+
let B = new Matrix(randomMatrix(size, size, cardinality));
86+
A.mmul(B);
6287
// Benchmark the multiplication
63-
yield () => do_not_optimize(A._mmulLowDensity(B));
88+
yield () => do_not_optimize(A.mmul(B));
6489
// Explicit cleanup
6590
do_not_optimize(A);
6691
do_not_optimize(B);
@@ -75,10 +100,10 @@ lineplot(() => {
75100
// Prepare matrices once
76101
let A = new SparseMatrix(randomMatrix(size, size, cardinality));
77102
let B = new SparseMatrix(randomMatrix(size, size, cardinality));
78-
103+
mmulMediumDensity(A, B);
79104
// Benchmark the multiplication
80105
yield () => {
81-
do_not_optimize(A._mmulMediumDensity(B));
106+
do_not_optimize(mmulMediumDensity(A, B));
82107
};
83108

84109
// Explicit cleanup
@@ -94,19 +119,19 @@ lineplot(() => {
94119
const results = await run({
95120
// Force GC between every benchmark
96121
gc: true,
97-
// // More samples for statistical significance
98-
// min_samples: 20,
99-
// max_samples: 200,
100-
// // Longer warmup to stabilize CPU state
101-
// warmup_samples: 10,
102-
// warmup_threshold: 100, // ms
122+
// More samples for statistical significance
123+
min_samples: 20,
124+
max_samples: 200,
125+
// Longer warmup to stabilize CPU state
126+
warmup_samples: 10,
127+
warmup_threshold: 100, // ms
103128
// Longer minimum time for stable measurements
104-
// min_cpu_time: 2000, // 2 seconds minimum
105-
// // Batch settings to reduce variance
106-
// batch_samples: 5,
107-
// batch_threshold: 10, // ms
108-
// // Enable colors
109-
// colors: true,
129+
min_cpu_time: 2000, // 2 seconds minimum
130+
// Batch settings to reduce variance
131+
batch_samples: 5,
132+
batch_threshold: 10, // ms
133+
// Enable colors
134+
colors: true,
110135
});
111136

112137
// Process and store results
@@ -134,6 +159,6 @@ for (const benchmark of results.benchmarks) {
134159

135160
// Save results to JSON file
136161
await writeFile(
137-
path.join(import.meta.dirname, `benchmark-results.json`),
162+
path.join(import.meta.dirname, `benchmark-results-${dimensions[0]}.json`),
138163
JSON.stringify(processedResults, null, 2),
139164
);

0 commit comments

Comments
 (0)