Last active
August 29, 2015 14:01
-
-
Save MidnightLightning/34441e726413aaef1767 to your computer and use it in GitHub Desktop.
Testing Big Integer performance
This file contains hidden or 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
{ | |
"name": "mathbuffer-test", | |
"version": "0.1.0", | |
"description": "Testing Big Integer implementation performance", | |
"main": "test.js", | |
"dependencies": { | |
"math-buffer": "~0.1.1", | |
"bigi": "~1.1.0" | |
}, | |
"author": "Brooks Boyd <[email protected]>" | |
} |
This file contains hidden or 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
'use strict'; | |
var BigMath = require('math-buffer'); | |
var BigInteger = require('bigi'); | |
function fromHex(s) { | |
return flipBuf(new Buffer(s, 'hex')); | |
}; | |
function arrayToHex(a) { return a.map(function(i) { return ('00'+i.toString(16)).slice(-2); }).join(''); }; | |
function LEhex(buf) { | |
return flipBuf(buf).toString('hex'); | |
} | |
function flipBuf(buf) { | |
var out = new Buffer(buf.length); | |
for (var i = 0; i < buf.length; i++) { | |
out[i] = buf[buf.length-i-1]; | |
} | |
return out; | |
} | |
function fromDec(s) { | |
var numDigits = s.length; | |
var TEN = BigMath.fromInt(10); | |
var out = BigMath.fromInt(0); | |
for (var i = 0; i < numDigits; i++) { | |
var value = parseInt(s[i]); | |
var power = numDigits-i-1; | |
var num = BigMath.fromInt(value); | |
for (var j = 0; j < power; j++) { | |
num = BigMath.multiply(num, TEN); | |
} | |
out = BigMath.add(out, num); | |
} | |
// Trim off zeroes | |
for (var i = out.length; i >= 0; i--) { | |
if (out[i] > 0) { | |
return out.slice(0, i+1); | |
} | |
} | |
return out.slice(0,1); | |
} | |
// Math Buffer testing | |
var q = fromHex('ffffffffffffffffffffffffffffffff7fffffff'); | |
var a = fromHex('ffffffffffffffffffffffffffffffff7ffffffc'); | |
var x = fromHex('4A96B5688EF57328'); | |
var TEN = new Buffer([10]); | |
for (var j = 0; j < 10; j++) { | |
console.time('buffer-multiply '+j); | |
for (var i = 0; i < 10; i++) { | |
var y = BigMath.multiply(x, TEN); | |
x = y; | |
} | |
console.timeEnd('buffer-multiply '+j); | |
} | |
x = fromHex('4A96B5688EF57328'); // Reset | |
for (var j = 0; j < 10; j++) { | |
console.time('buffer-square '+j); | |
for (var i = 0; i < 10; i++) { | |
var y = BigMath.square(x); | |
//x = y; | |
} | |
console.timeEnd('buffer-square '+j); | |
} | |
x = fromHex('4A96B5688EF57328'); // Reset | |
for (var j = 0; j < 10; j++) { | |
console.time('buffer-mod '+j); | |
for (var i = 0; i < 10; i++) { | |
var y = BigMath.multiply(x, TEN); | |
var z = BigMath.divide(y, q); | |
x = y; | |
} | |
console.timeEnd('buffer-mod '+j); | |
} | |
// Bigi testing | |
var q = new BigInteger('ffffffffffffffffffffffffffffffff7fffffff', 16); | |
var a = new BigInteger('ffffffffffffffffffffffffffffffff7ffffffc', 16); | |
var x = new BigInteger('4A96B5688EF57328', 16); | |
var TEN = new BigInteger('10'); | |
for (var j = 0; j < 10; j++) { | |
console.time('bigi-multiply '+j); | |
for (var i = 0; i < 10; i++) { | |
var y = x.multiply(TEN); | |
x = y; | |
} | |
console.timeEnd('bigi-multiply '+j); | |
} | |
x = new BigInteger('4A96B5688EF57328', 16); // Reset | |
for (var j = 0; j < 10; j++) { | |
console.time('bigi-square '+j); | |
for (var i = 0; i < 10; i++) { | |
var y = x.pow(2); | |
//x = y; | |
} | |
console.timeEnd('bigi-square '+j); | |
} | |
x = new BigInteger('4A96B5688EF57328', 16); // Reset | |
for (var j = 0; j < 10; j++) { | |
console.time('bigi-mod '+j); | |
for (var i = 0; i < 10; i++) { | |
var y = x.multiply(TEN); | |
var z = y.remainder(q); | |
x = y; | |
} | |
console.timeEnd('bigi-mod '+j); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment