math.js Typed Arrays

node v11.14.0
version: 1.0.3
endpointsharetweet
var math = require("mathjs") var testArray1 = Array.from({ length: 1000000 }, () => Math.floor(Math.random() * 2000000) ); let output; // Warm up for(var i = 0; i < 50; i++) { output = math.multiply(testArray1, Math.random()) } console.time('built-in') output = math.multiply(testArray1, Math.random()) console.timeEnd('built-in') math.typed.addType({ name: 'Float32Array', test: function (x) { return x instanceof Float32Array } }) // Override the function const multiply = math.typed('multiply', { 'Array, number': function (a, b) { const outArray = new Array(a.length); for(var i = 0; i < a.length; i++){ outArray[i] = a[i] * b; } return outArray; }, 'Float32Array, number': function (a, b) { const outArray = new Float32Array(a.length); for(var i = 0; i < a.length; i++){ outArray[i] = a[i] * b; } return outArray; } }) math.import({ multiply: multiply }, { override: true } ) // Warm up for(var i = 0; i < 50; i++) { output = math.multiply(testArray1, Math.random()) } console.time('override') output = math.multiply(testArray1, Math.random()) console.timeEnd('override') const typedArray1 = Float32Array.from(testArray1); // Warm up for(var i = 0; i < 50; i++) { output = math.multiply(typedArray1, Math.random()) } console.time('override-typed') output = math.multiply(typedArray1, Math.random()) console.timeEnd('override-typed')
Loading…

no comments

    sign in to comment