From 190176c5fef89d78525be929c9753823645b8ce2 Mon Sep 17 00:00:00 2001 From: Luc Patiny Date: Wed, 21 Aug 2024 17:20:45 +0200 Subject: [PATCH] test: add some basic code example for benchmark --- benchmark/denseMatrix.mjs | 19 ++++++++++++++ benchmark/sparseMatrix.byhand.mjs | 41 +++++++++++++++++++++++++++++++ benchmark/sparseMatrix.mjs | 30 ++++++++++++++++++++++ package.json | 1 + 4 files changed, 91 insertions(+) create mode 100644 benchmark/denseMatrix.mjs create mode 100644 benchmark/sparseMatrix.byhand.mjs create mode 100644 benchmark/sparseMatrix.mjs diff --git a/benchmark/denseMatrix.mjs b/benchmark/denseMatrix.mjs new file mode 100644 index 0000000..22d48dc --- /dev/null +++ b/benchmark/denseMatrix.mjs @@ -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'); + diff --git a/benchmark/sparseMatrix.byhand.mjs b/benchmark/sparseMatrix.byhand.mjs new file mode 100644 index 0000000..ee8c42a --- /dev/null +++ b/benchmark/sparseMatrix.byhand.mjs @@ -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); + } + } +} \ No newline at end of file diff --git a/benchmark/sparseMatrix.mjs b/benchmark/sparseMatrix.mjs new file mode 100644 index 0000000..777c436 --- /dev/null +++ b/benchmark/sparseMatrix.mjs @@ -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); + } + } +} \ No newline at end of file diff --git a/package.json b/package.json index 5c02fe8..dd8267a 100644 --- a/package.json +++ b/package.json @@ -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"