|
| 1 | +import { bench, do_not_optimize, lineplot, run } from 'mitata'; |
| 2 | +import { xSequentialFillFromStep } from 'ml-spectra-processing'; |
| 3 | + |
| 4 | +import { SparseMatrix } from '../src/index.js'; |
| 5 | + |
| 6 | +import { SparseMatrix as SparseMatrixOld } from './class/SparseMatrixOld.js'; |
| 7 | +import { randomMatrix } from './utils/randomMatrix.js'; |
| 8 | + |
| 9 | +const density = 0.02; // Fixed density for this comparison; |
| 10 | + |
| 11 | +// Prepare matrices once |
| 12 | +const sizes = Array.from( |
| 13 | + xSequentialFillFromStep({ from: 4, step: 4, size: 13 }), |
| 14 | +); |
| 15 | +lineplot(() => { |
| 16 | + bench('Sparse.mmul($size)', function* (ctx) { |
| 17 | + const size = ctx.get('size'); |
| 18 | + |
| 19 | + // Prepare matrices once |
| 20 | + const A = new SparseMatrix(randomMatrix(size, size, density)); |
| 21 | + const B = new SparseMatrix(randomMatrix(size, size, density)); |
| 22 | + // Benchmark the multiplication |
| 23 | + yield () => do_not_optimize(A.mmul(B)); |
| 24 | + }) |
| 25 | + .gc('inner') |
| 26 | + .args('size', sizes); // 16, 32, 64, 128, 256 |
| 27 | + |
| 28 | + bench('SparseOld.mmul($size)', function* (ctx) { |
| 29 | + const size = ctx.get('size'); |
| 30 | + const A = randomMatrix(size, size, density); |
| 31 | + const B = randomMatrix(size, size, density); |
| 32 | + const AOld = new SparseMatrixOld(A); |
| 33 | + const BOld = new SparseMatrixOld(B); |
| 34 | + |
| 35 | + // Benchmark the multiplication |
| 36 | + yield () => do_not_optimize(AOld.mmul(BOld)); |
| 37 | + }) |
| 38 | + .gc('inner') |
| 39 | + .args('size', sizes); |
| 40 | + |
| 41 | + // bench('Dense.mmul($size)', function* (ctx) { |
| 42 | + // const size = ctx.get('size'); |
| 43 | + |
| 44 | + // // Prepare matrices once |
| 45 | + // const A = randomMatrix(size, size, density); |
| 46 | + // const B = randomMatrix(size, size, density); |
| 47 | + // const ADense = new Matrix(A); |
| 48 | + // const BDense = new Matrix(B); |
| 49 | + |
| 50 | + // // Benchmark the multiplication |
| 51 | + // yield () => do_not_optimize(ADense.mmul(BDense)); |
| 52 | + // }).gc('inner').range('size', min, max, multiplier); |
| 53 | +}); |
| 54 | + |
| 55 | +await run(); |
0 commit comments