Skip to content

Commit 190176c

Browse files
committed
test: add some basic code example for benchmark
1 parent 727ad3b commit 190176c

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

benchmark/denseMatrix.mjs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Matrix } from 'ml-matrix';
2+
3+
let size = 200;
4+
5+
console.time('dense');
6+
const matrix = new Matrix(size, size).fill(1);
7+
const matrix2 = new Matrix(size, size).fill(1);
8+
9+
const product = matrix.mmul(matrix2);
10+
// sum of all the elements
11+
let sum = 0;
12+
for (let i = 0; i < size; i++) {
13+
for (let j = 0; j < size; j++) {
14+
sum += product.get(i, j);
15+
}
16+
}
17+
console.log(sum)
18+
console.timeEnd('dense');
19+

benchmark/sparseMatrix.byhand.mjs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
3+
import { SparseMatrix } from '../lib/index.js';
4+
5+
let size = 200;
6+
7+
8+
9+
console.time('dense');
10+
11+
const matrix1 = new SparseMatrix(size, size);
12+
fillSparseMatrix(matrix1)
13+
14+
const matrix2 = new SparseMatrix(size, size);
15+
fillSparseMatrix(matrix2)
16+
17+
const product = new SparseMatrix(size, size);
18+
for (let i = 0; i < size; i++) {
19+
for (let j = 0; j < size; j++) {
20+
for (let k = 0; k < size; k++) {
21+
product.set(i, j, product.get(i, j) + matrix1.get(i, k) * matrix2.get(k, j));
22+
}
23+
}
24+
}
25+
26+
let sum = 0;
27+
for (let i = 0; i < size; i++) {
28+
for (let j = 0; j < size; j++) {
29+
sum += product.get(i, j);
30+
}
31+
}
32+
console.timeEnd('dense');
33+
console.log(sum)
34+
35+
function fillSparseMatrix(matrix) {
36+
for (let row = 0; row < matrix.rows; row++) {
37+
for (let column = 0; column < matrix.columns; column++) {
38+
matrix.set(row, column, 1);
39+
}
40+
}
41+
}

benchmark/sparseMatrix.mjs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { SparseMatrix } from '../lib/index.js';
2+
3+
let size = 200;
4+
5+
console.time('dense');
6+
7+
const matrix = new SparseMatrix(size, size);
8+
fillSparseMatrix(matrix)
9+
10+
const matrix2 = new SparseMatrix(size, size);
11+
fillSparseMatrix(matrix2)
12+
13+
const product = matrix.mmul(matrix2);
14+
// sum of all the elements
15+
let sum = 0;
16+
for (let i = 0; i < size; i++) {
17+
for (let j = 0; j < size; j++) {
18+
sum += product.get(i, j);
19+
}
20+
}
21+
console.timeEnd('dense');
22+
console.log(sum)
23+
24+
function fillSparseMatrix(matrix) {
25+
for (let row = 0; row < matrix.rows; row++) {
26+
for (let column = 0; column < matrix.columns; column++) {
27+
matrix.set(row, column, 1);
28+
}
29+
}
30+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"@vitest/coverage-v8": "^2.0.5",
3838
"eslint": "^8.10.0",
3939
"eslint-config-cheminfo": "^11.1.1",
40+
"ml-matrix": "^6.11.1",
4041
"prettier": "^3.3.3",
4142
"rollup": "^4.21.0",
4243
"vitest": "^2.0.5"

0 commit comments

Comments
 (0)