# Matrix multiplication with asm.js

## JavaScript performance comparison

Revision 3 of this test case created by Alexander Gessler

## Info

Trying super-fast matrix multiplication with asm.js by using a huge pool of matrices exclusively used as asm.js heap. User code indexes matrices by their position in that pool.

github.com/acgessler Matrix mult code from Brandon Jones's Gl-matrix

## Preparation code

`` <script>  Benchmark.prototype.setup = function() {    // trying super-fast matrix multiplication with asm.js by using a huge pool    // of matrices exclusively used as asm.js heap. User code indexes matrices    // by their position in that pool.        // Alexander Gessler, 2013 (github.com/acgessler)        var COUNT = 100;    var elements = 16 * COUNT;        var matrixpool = new Float32Array((Math.ceil(elements / 1024.0) * 1024) | 0); // multiple of 4096 bytes    console.log('mpool: ' + matrixpool.length);        function mat_mod(stdlib, foreign, heap) {      "use asm";          var m = new stdlib.Float32Array(heap);          function multiply(idx_a, idx_b, idx_dest) {        idx_a = idx_a | 0;        idx_b = idx_b | 0;        idx_dest = idx_dest | 0;            idx_a = idx_a << 4;        idx_b = idx_b << 4;        idx_dest = idx_dest << 4;            for (var i = 0 | 0; i < 4; i = (i + 1) | 0) {          for (var j = 0 | 0; j < 4; j = (j + 1) | 0) {            var c = +0.0;            for (var k = 0 | 0; k < 4; k = (k + 1) | 0) {              c = +(c + m[(idx_a + (i << 2) + k) | 0] * m[(idx_b + (k << 2) + j) | 0]);            }            m[(idx_dest + 4 * i + j) | 0] = c;          }        }      }          return {        multiply: multiply      }    }        var mod = mat_mod(window, undefined, matrixpool);            // fill all matrices with values    for (var i = matrixpool - 1; i >= 0; --i) {      matrixpool[i] = i % 2 ? 0.5 : 2.0;    }  };</script>``

## Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
matrix multiplication
``// multiply lots of matricesfor (var k = 0; k < 5; ++k) {  for (var i = 0; i < COUNT; ++i) {    for (var j = COUNT - 1; j >= 0; --j) {      mod.multiply(i, j, (i + j) % COUNT);    }  }}``
pending…
same as before
``// multiply lots of matricesfor (var k = 0; k < 5; ++k) {  for (var i = 0; i < COUNT; ++i) {    for (var j = COUNT - 1; j >= 0; --j) {      mod.multiply(i, j, (i + j) % COUNT);    }  }}``
pending…

## Revisions

You can edit these tests or add even more tests to this page by appending `/edit` to the URL. Here’s a list of current revisions for this page:

• Revision 1: published by Alexander Gessler
• Revision 2: published
• Revision 3: published by Alexander Gessler
• Revision 4: published by Alexander Gessler
• Revision 5: published by Alexander Gessler
• Revision 6: published
• Revision 7: published

Comment form temporarily disabled.