1
1
import { SparseMatrix } from '../src/index.js' ;
2
2
import { Matrix } from 'ml-matrix' ;
3
3
import { SparseMatrix as SparseMatrixOld } from './class/SparseMatrixOld.js' ;
4
+ // import { SparseMatrix as SparseMatrixOld } from '../src/Elements.js';
4
5
import fs from 'fs' ;
5
6
6
7
function randomSparseMatrix ( rows , cols , density = 0.01 ) {
7
- const matrix = [ ] ;
8
- for ( let i = 0 ; i < rows ; i ++ ) {
9
- const row = new Array ( cols ) . fill ( 0 ) ;
10
- for ( let j = 0 ; j < cols ; j ++ ) {
11
- if ( Math . random ( ) < density ) {
12
- row [ j ] = Math . random ( ) * 10 ;
13
- }
14
- }
15
- matrix . push ( row ) ;
8
+ const total = rows * cols ;
9
+ const cardinality = Math . round ( total * density ) ;
10
+ const positions = new Set ( ) ;
11
+
12
+ // Generate unique random positions
13
+ while ( positions . size < cardinality ) {
14
+ positions . add ( Math . floor ( Math . random ( ) * total ) ) ;
16
15
}
16
+
17
+ // Build the matrix with zeros
18
+ const matrix = Array . from ( { length : rows } , ( ) => new Float64Array ( cols ) ) ;
19
+
20
+ // Assign random values to the selected positions
21
+ for ( const pos of positions ) {
22
+ const i = Math . floor ( pos / cols ) ;
23
+ const j = pos % cols ;
24
+ matrix [ i ] [ j ] = Math . random ( ) * 10 ;
25
+ }
26
+
17
27
return new SparseMatrix ( matrix ) ;
18
28
}
19
29
20
- function benchmark ( fn , label , iterations = 5 ) {
30
+ function benchmark ( fn , label , iterations = 5 , logIt = false ) {
21
31
const times = [ ] ;
22
32
for ( let i = 0 ; i < iterations ; i ++ ) {
23
33
const t0 = performance . now ( ) ;
@@ -26,7 +36,9 @@ function benchmark(fn, label, iterations = 5) {
26
36
times . push ( t1 - t0 ) ;
27
37
}
28
38
const avg = times . reduce ( ( a , b ) => a + b , 0 ) / times . length ;
29
- console . log ( `${ label } : avg ${ avg . toFixed ( 2 ) } ms over ${ iterations } runs` ) ;
39
+ if ( logIt ) {
40
+ console . log ( `${ label } : avg ${ avg . toFixed ( 2 ) } ms over ${ iterations } runs` ) ;
41
+ }
30
42
return avg ;
31
43
}
32
44
@@ -53,9 +65,9 @@ function printWinner(label1, avg1, label2, avg2) {
53
65
}
54
66
55
67
function runBenchmarks ( ) {
56
- const m = 128 ;
57
- const n = 128 ;
58
- const p = 128 ;
68
+ const m = 256 ;
69
+ const n = 256 ;
70
+ const p = 256 ;
59
71
const densityA = 0.01 ;
60
72
const densityB = 0.01 ;
61
73
@@ -137,8 +149,9 @@ function runBenchmarks() {
137
149
}
138
150
139
151
function runSizeSweepBenchmark ( ) {
140
- const sizes = [ 8 , 16 , 32 , 64 , 128 ] ;
141
- const densities = [ 0.01 , 0.015 , 0.02 , 0.025 , 0.03 , 0.35 ] ;
152
+ const nbIterations = 3 ;
153
+ const sizes = [ 32 , 64 , 128 , 256 ] ;
154
+ const densities = [ 0.01 , 0.015 , 0.02 , 0.025 , 0.03 ] ;
142
155
const results = [ ] ;
143
156
144
157
for ( const densityA of densities ) {
@@ -165,29 +178,33 @@ function runSizeSweepBenchmark() {
165
178
A . mmul ( B ) ;
166
179
} ,
167
180
'mmulNew' ,
168
- 3 ,
181
+ nbIterations ,
169
182
) ;
170
183
171
184
const mmulAvg = benchmark (
172
185
( ) => {
173
186
AOld . mmul ( BOld ) ;
174
187
} ,
175
188
'mmul' ,
176
- 3 ,
189
+ nbIterations ,
177
190
) ;
178
191
179
- const denseAvg = benchmark ( ( ) => {
180
- denseA . mmul ( denseB ) , 'denseMatrix' , 3 ;
181
- } ) ;
192
+ const denseAvg = benchmark (
193
+ ( ) => {
194
+ denseA . mmul ( denseB ) ;
195
+ } ,
196
+ 'denseMatrix' ,
197
+ nbIterations ,
198
+ ) ;
182
199
183
200
results . push ( {
184
201
densityA,
185
202
densityB,
186
203
A_shape : [ m , n ] ,
187
204
B_shape : [ n , p ] ,
188
205
dense : denseAvg ,
189
- mmulNew : mmulNewAvg ,
190
- mmul : mmulAvg ,
206
+ new : mmulNewAvg ,
207
+ old : mmulAvg ,
191
208
} ) ;
192
209
}
193
210
}
@@ -202,6 +219,6 @@ function runSizeSweepBenchmark() {
202
219
console . log ( 'Size sweep benchmark results saved to size_sweep_results.json' ) ;
203
220
}
204
221
205
- runBenchmarks ( ) ;
222
+ // runBenchmarks();
206
223
// Uncomment to run the size sweep benchmark
207
- // runSizeSweepBenchmark();
224
+ runSizeSweepBenchmark ( ) ;
0 commit comments