Skip to content

Commit

Permalink
test: add some basic code example for benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
lpatiny committed Aug 21, 2024
1 parent 727ad3b commit 190176c
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
19 changes: 19 additions & 0 deletions benchmark/denseMatrix.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Matrix } from 'ml-matrix';

let size = 200;

console.time('dense');
const matrix = new Matrix(size, size).fill(1);
const matrix2 = new Matrix(size, size).fill(1);

const product = matrix.mmul(matrix2);
// sum of all the elements
let sum = 0;
for (let i = 0; i < size; i++) {
for (let j = 0; j < size; j++) {
sum += product.get(i, j);
}
}
console.log(sum)
console.timeEnd('dense');

41 changes: 41 additions & 0 deletions benchmark/sparseMatrix.byhand.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@


import { SparseMatrix } from '../lib/index.js';

let size = 200;



console.time('dense');

const matrix1 = new SparseMatrix(size, size);
fillSparseMatrix(matrix1)

const matrix2 = new SparseMatrix(size, size);
fillSparseMatrix(matrix2)

const product = new SparseMatrix(size, size);
for (let i = 0; i < size; i++) {
for (let j = 0; j < size; j++) {
for (let k = 0; k < size; k++) {
product.set(i, j, product.get(i, j) + matrix1.get(i, k) * matrix2.get(k, j));
}
}
}

let sum = 0;
for (let i = 0; i < size; i++) {
for (let j = 0; j < size; j++) {
sum += product.get(i, j);
}
}
console.timeEnd('dense');
console.log(sum)

function fillSparseMatrix(matrix) {
for (let row = 0; row < matrix.rows; row++) {
for (let column = 0; column < matrix.columns; column++) {
matrix.set(row, column, 1);
}
}
}
30 changes: 30 additions & 0 deletions benchmark/sparseMatrix.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { SparseMatrix } from '../lib/index.js';

let size = 200;

console.time('dense');

const matrix = new SparseMatrix(size, size);
fillSparseMatrix(matrix)

const matrix2 = new SparseMatrix(size, size);
fillSparseMatrix(matrix2)

const product = matrix.mmul(matrix2);
// sum of all the elements
let sum = 0;
for (let i = 0; i < size; i++) {
for (let j = 0; j < size; j++) {
sum += product.get(i, j);
}
}
console.timeEnd('dense');
console.log(sum)

function fillSparseMatrix(matrix) {
for (let row = 0; row < matrix.rows; row++) {
for (let column = 0; column < matrix.columns; column++) {
matrix.set(row, column, 1);
}
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@vitest/coverage-v8": "^2.0.5",
"eslint": "^8.10.0",
"eslint-config-cheminfo": "^11.1.1",
"ml-matrix": "^6.11.1",
"prettier": "^3.3.3",
"rollup": "^4.21.0",
"vitest": "^2.0.5"
Expand Down

0 comments on commit 190176c

Please sign in to comment.