Skip to content

Instantly share code, notes, and snippets.

@mashihua
Created December 4, 2014 14:00
Show Gist options
  • Save mashihua/6bec4f13207d2bcabd26 to your computer and use it in GitHub Desktop.
Save mashihua/6bec4f13207d2bcabd26 to your computer and use it in GitHub Desktop.
different performance
/*
Different performance for https://github.com/ceejbot/xx-bloom/blob/master/lib/bloom.js
Output (2.4 GHz Intel Core i7):
setbit cost: 24304ms
fastSetbit cost: 1ms
*/
var assert = require('assert')
function setbit(bit){
var pos = 0
var shift = bit
while (shift > 7){
pos++
shift -= 8
}
return [pos, shift]
}
function fastSetbit(bit){
var pos = bit >> 3
var shift = bit % 8
return [pos, shift]
}
var arr = []
for (var i = 0 ; i < 10000; i ++){
arr.push(Math.floor( Math.random() * 3000 * 10000))
}
arr.forEach(function(n){
var s = setbit(n)
var s1 = fastSetbit(n)
assert.equal(s[0], s1[0], 's[0]:' + s[0] + '=s1[0]' + s1[0])
assert.equal(s[1], s1[1], 's[1]:' + s[1] + '=s1[1]' + s1[1])
})
var start = new Date
arr.forEach(function(n){
setbit(n)
})
console.log('setbit cost: %dms', new Date - start)
var start = new Date
arr.forEach(function(n){
fastSetbit(n)
})
console.log('fastSetbit cost: %dms', new Date - start)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment