Improved performance of bufferReader.readUInt64LEBN()

This commit is contained in:
Braydon Fuller 2015-06-26 14:02:26 -04:00
parent 9ab6eefef3
commit a3dee0695d
2 changed files with 41 additions and 5 deletions

View File

@ -14,6 +14,45 @@ console.log('Benchmarking Block/Transaction Serialization');
console.log('---------------------------------------');
async.series([
function(next) {
var buffers = [];
console.log('Generating Random Test Data...');
for (var i = 0; i < 100; i++) {
var br = new bitcore.encoding.BufferWriter();
var num = Math.round(Math.random() * 10000000000000);
br.writeUInt64LEBN(new bitcore.crypto.BN(num));
buffers.push(br.toBuffer());
}
var c = 0;
var bn;
function readUInt64LEBN() {
if (c >= buffers.length) {
c = 0;
}
var buf = buffers[c];
var br = new bitcore.encoding.BufferReader(buf);
bn = br.readUInt64LEBN();
c++;
}
console.log('Starting benchmark...');
var suite = new benchmark.Suite();
suite.add('bufferReader.readUInt64LEBN()', readUInt64LEBN, {maxTime: maxTime});
suite
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Done');
console.log('----------------------------------------------------------------------');
next();
})
.run();
},
function(next) {
var block1;

View File

@ -91,11 +91,8 @@ BufferReader.prototype.readUInt64BEBN = function() {
};
BufferReader.prototype.readUInt64LEBN = function() {
var buf = this.buf.slice(this.pos, this.pos + 8);
var reversebuf = BufferReader({
buf: buf
}).readReverse();
var bn = BN.fromBuffer(reversebuf);
var data = this.buf.slice(this.pos, this.pos + 8).toJSON().data;
var bn = new BN(data, 10, 'le');
this.pos = this.pos + 8;
return bn;
};