Created
December 6, 2019 09:48
-
-
Save davidaurelio/37db2f677ebe9cfe019036fae0a18fa6 to your computer and use it in GitHub Desktop.
count bits (http://jsbench.github.io/#37db2f677ebe9cfe019036fae0a18fa6) #jsbench #jsperf
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>count bits</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script> | |
<script src="./suite.js"></script> | |
</head> | |
<body> | |
<h1>Open the console to view the results</h1> | |
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2> | |
</body> | |
</html> |
This file contains 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"; | |
(function (factory) { | |
if (typeof Benchmark !== "undefined") { | |
factory(Benchmark); | |
} else { | |
factory(require("benchmark")); | |
} | |
})(function (Benchmark) { | |
var suite = new Benchmark.Suite; | |
Benchmark.prototype.setup = function () { | |
const table = new Int32Array(256); | |
for (let i = 0; i < table.length; ++i) table[i] = (i & 1) + table[i / 2]; | |
function tableLookup(n) { | |
return table[n & 0xff] + table[(n >>> 8) & 0xff] + table[(n >>> 16) & 0xff] + table[n >>> 24]; | |
} | |
function bitFiddling(v) { | |
v = v - ((v >>> 1) & 0x55555555); | |
v = (v & 0x33333333) + ((v >>> 2) & 0x33333333); | |
return ((v + (v >>> 4) & 0xF0F0F0F) * 0x1010101) >>> 24; | |
} | |
function naive(v) { | |
let bits = 0; | |
while (v) { | |
bits += v & 1; | |
v >>>= 1; | |
} | |
return bits; | |
} | |
function assert(condition, message) { | |
if (!condition) throw new Error(message); | |
} | |
const int32s = [ | |
0, | |
1, -1, -2, -1061915544, -1555347375, | |
1120351692, -563428345, | |
863023321, -1455766619, | |
1090078114, | |
1545926896, | |
353401604, | |
1926705176, | |
2066963906, | |
1310154921, | |
794500772, -293685467, | |
635914052, -696870554, -685960877, -1564768615, -908098734, | |
1997198644, -1696246300, | |
787069003, -773953043, -235755836, | |
1825707196, -1413376694, -1332116542, | |
810036644, -1837823121, -722610458, -97543692, | |
1086944003, -1247712344, | |
104166236, -705871200, -544442644, -67279285, | |
842775500, | |
798080844, -557572388, -262737337, -1203086408, -127658972, -1045852068, -1457067762, -1410023778, -1096304808, -315755944, | |
889378202, | |
34525992, | |
773371328, -521213376, -495050220, | |
385953520, | |
1720432991, | |
2082574462, | |
1062332896, | |
769181290, | |
144354176, | |
1145499621, -653353083, | |
1488676250, | |
408064013, | |
219026518, -850128992, | |
1989039182, | |
503186529, -869520878, | |
1344526480, -331372343, | |
22089576, | |
326802570, -1798031256, | |
2083475736, -1045800478, -429869316, -287023120, -1897640640, -108797603, -509689151, -1979205164, -591203265, -1236105036, | |
1730724752, -1479716844, -1509702343, | |
1822319432, -1993357146, -664865913, -1269187636, | |
484933660, -968168341, | |
1387535965, -1226749615, -574704542, | |
1162372512, -1852546033, | |
991446254, -266798208, | |
1868632965, | |
1718674624, -1342068840, | |
334601446, | |
1845086418, | |
2078681314, -1572654006, -909790936, -1051593787, -727234566, -220199360, -1238855763, | |
962218858, | |
193290686, | |
1320017958, | |
477544672, | |
699625336, | |
1876829198, -405126864, | |
1605412660, -1679238464, -1489968018, | |
692546469, -559532124, | |
1511711040 | |
]; | |
const expected = int32s.map(naive); | |
const results = int32s.slice().fill(NaN); | |
}; | |
Benchmark.prototype.teardown = function () { | |
int32s.forEach((n, i) => assert(results[i] === expected[i], `Wrong result for ${n}`)); | |
}; | |
suite.add("for (let i = 0, n = int32s.length; i < n; ++n)", function () { | |
for (let i = 0, n = int32s.length; i < n; ++n) | |
results[i] = tableLookup(int32s[i]); | |
}); | |
suite.add("for (let i = 0, n = int32s.length; i < n; ++n)", function () { | |
for (let i = 0, n = int32s.length; i < n; ++n) | |
results[i] = bitFiddling(int32s[i]); | |
}); | |
suite.add("for (let i = 0, n = int32s.length; i < n; ++n)", function () { | |
for (let i = 0, n = int32s.length; i < n; ++n) | |
results[i] = naive(int32s[i]); | |
}); | |
suite.on("cycle", function (evt) { | |
console.log(" - " + evt.target); | |
}); | |
suite.on("complete", function (evt) { | |
console.log(new Array(30).join("-")); | |
var results = evt.currentTarget.sort(function (a, b) { | |
return b.hz - a.hz; | |
}); | |
results.forEach(function (item) { | |
console.log((idx + 1) + ". " + item); | |
}); | |
}); | |
console.log("count bits"); | |
console.log(new Array(30).join("-")); | |
suite.run(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment