-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add some basic code example for benchmark
- Loading branch information
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters