<!--your preparation HTML code goes here-->
var An = 200, Ann = An*An;
var A = new Float64Array(Ann);
for(let i=0;i<Ann;i++)A[i]=Math.random()-0.5;
const uFlat = new Float64Array(A), vFlat = new Float64Array(Ann);
let m = An, n = An;
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
vFlat[j * m + i] = uFlat[i * n + j]
}
}
const uFlat = new Float64Array(A), vFlat = new Float64Array(Ann);
let m = An, n = An;
let vIndex = 0;
for (let j = 0; j < n; j++) {
let uIndex = j;
for (let i = 0; i < m; i++) {
vFlat[vIndex++] = uFlat[uIndex];
uIndex += n;
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
transpose simple | |
transpose optimized |
Test name | Executions per second |
---|---|
transpose simple | 11451.4 Ops/sec |
transpose optimized | 11990.2 Ops/sec |
The benchmark titled "Matrix Transpose" focuses on evaluating the performance of different approaches to transposing a matrix represented as a flattened array in JavaScript. The benchmark prepares a random matrix through JavaScript and compares two techniques for transposing that matrix: a simple method and an optimized method.
Transpose Simple
const uFlat = new Float64Array(A), vFlat = new Float64Array(Ann);
let m = An, n = An;
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
vFlat[j * m + i] = uFlat[i * n + j];
}
}
uFlat
and write them into the output array vFlat
based on the transpose logic.Transpose Optimized
const uFlat = new Float64Array(A), vFlat = new Float64Array(Ann);
let m = An, n = An;
let vIndex = 0;
for (let j = 0; j < n; j++) {
let uIndex = j;
for (let i = 0; i < m; i++) {
vFlat[vIndex++] = uFlat[uIndex];
uIndex += n;
}
}
uFlat
in a more efficient manner by incrementing uIndex
in a way that minimizes repetitive calculations.uIndex
incrementally, we avoid the need to perform the multiplication inside the loop.Library and Language Features:
In this benchmark, there are no external libraries used; the code is purely JavaScript. The main data structure utilized is the Float64Array
, which represents an array of 64-bit floating-point numbers, specifically designed for handling numeric data efficiently in WebAssembly contexts and performance-critical applications.
Alternatives:
math.js
, numeric.js
, or ndarray
can be utilized. These provide built-in functions for matrix operations including transposing, which can be more user-friendly but may have overhead due to the additional abstraction.Float64Array
, which is beneficial for performance due to typed arrays being more efficient in memory usage and operations. However, for smaller data sets or simpler applications, regular JavaScript arrays might suffice and be easier to work with.This benchmark provides insight into how different matrix transpose techniques can influence performance in JavaScript, illuminating the trade-offs between readability and optimization. Understanding these differences can help software engineers choose the appropriate approach based on their application needs while considering the context of using JavaScript for performance-critical applications.