Skip to content

optimize toBitArray function#212

Merged
fanatid merged 1 commit intoindutny:masterfrom
fanatid:optimize-tobitarray
Jul 7, 2019
Merged

optimize toBitArray function#212
fanatid merged 1 commit intoindutny:masterfrom
fanatid:optimize-tobitarray

Conversation

@fanatid
Copy link
Collaborator

@fanatid fanatid commented Jan 27, 2019

Remove extra bitwise operator

performance:

  function toBitArray1 (num) {
    var w = new Array(num.bitLength());

    for (var bit = 0; bit < w.length; bit++) {
      var off = (bit / 26) | 0;
      var wbit = bit % 26;

      w[bit] = (num.words[off] >>> wbit) & 0x01;
      // w[bit] = (num.words[off] & (1 << wbit)) >>> wbit;
    }

    return w;
  }

  function toBitArray2 (num) {
    var w = new Array(num.bitLength());

    for (var bit = 0; bit < w.length; bit++) {
      var off = (bit / 26) | 0;
      var wbit = bit % 26;

      // w[bit] = (num.words[off] >>> wbit) & 0x01;
      w[bit] = (num.words[off] & (1 << wbit)) >>> wbit;
    }

    return w;
  }

const z = new BN(require('crypto').randomBytes(32))
console.time('toBitArray1')
for (let i = 0; i < 1e5; ++i) toBitArray1(z)
console.timeEnd('toBitArray1')
console.time('toBitArray2')
for (let i = 0; i < 1e5; ++i) toBitArray2(z)
console.timeEnd('toBitArray2')

// toBitArray1: 85.165ms
// toBitArray2: 96.260ms

Remove extra bitwise operator
chjj added a commit to bcoin-org/bcrypto that referenced this pull request Mar 28, 2019
@fanatid fanatid merged commit 7ba7a8c into indutny:master Jul 7, 2019
@fanatid fanatid deleted the optimize-tobitarray branch July 7, 2019 20:07
@fanatid fanatid mentioned this pull request Dec 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant