count leading zeros
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
function lead(m) {
var c, i;
c = 1 << 31;
for (i = 0; i < 32; i += 1) {
if ((c & m) !== 0) return i;
c >>>= 1;
}
return 32;
}
function lead2(input) {
var output = 0,
curbyte = 0;
while(true) { // emulate goto in JS using the break statement :D
curbyte = input >>> 24;
if (curbyte) break;
output += 8;
curbyte = input >>> 16;
if (curbyte & 0xff) break;
output += 8;
curbyte = input >> 8;
if (curbyte & 0xff) break;
output += 8;
curbyte = input;
if (curbyte & 0xff) break;
output += 8;
return output;
}
if (!(curbyte & 0xf0))
output += 4;
else
curbyte >>>= 4;
if (curbyte & 0x8)
return output;
if (curbyte & 0x4)
return output + 1;
if (curbyte & 0x2)
return output + 2;
if (curbyte & 0x1)
return output + 3;
/* shouldn't get here: */
return output + 4;
}
function lead3(x) {
return 32 - (x).toString(2).length;
}
function clz(x) {
if (x < 0) {
return 0
} else if (x == 0) {
return 32
} else {
return 31 - ((Math.log(x) / Math.LN2) >> 0)
}
}
function clz2(x) {
if (x < 1) {
if (x == 0) {
return 32
} else {
return 0
}
} else {
return 32 - ((Math.log(x) / Math.LN2) >> 0)
}
}
function clz3(n) {
if (n & 0xFFFF0000) {
if (n & 0xFF000000) {
if (n & 0x0F0000000) {
if (n & 0x0C0000000) {
return (n & 0x080000000) ? 32 : 31
} else {
return (n & 0x020000000) ? 30 : 29
}
} else {
if (n & 0x00C000000) {
return (n & 0x008000000) ? 28 : 27
} else {
return (n & 0x002000000) ? 26 : 25
}
}
} else {
if (n & 0x000F00000) {
if (n & 0x000C00000) {
return (n & 0x000800000) ? 24 : 23
} else {
return (n & 0x000200000) ? 22 : 21
}
} else {
if (n & 0x0000C0000) {
return (n & 0x000080000) ? 20 : 19
} else {
return (n & 0x000020000) ? 18 : 17
}
}
}
} else {
if (n & 0x00000FF00) {
if (n & 0x00000F000) {
if (n & 0x00000C000) {
return (n & 0x000008000) ? 16 : 15
} else {
return (n & 0x000002000) ? 14 : 13
}
} else {
if (n & 0x000000C00) {
return (n & 0x000000800) ? 12 : 11
} else {
return (n & 0x000000200) ? 10 : 9
}
}
} else {
if (n & 0x0000000F0) {
if (n & 0x0000000C0) {
return (n & 0x000000080) ? 8 : 7
} else {
return (n & 0x000000020) ? 6 : 5
}
} else {
if (n & 0x00000000C) {
return (n & 0x000000008) ? 4 : 3
} else {
return (n & 0x000000002) ? 2 : (n ? 1 : 0)
}
}
}
}
}
var LZ = [8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
var Buffer = new ArrayBuffer(8)
var I = new Int32Array(Buffer)
var B = new Uint8Array(Buffer)
function lookup(x) {
I[0] = x
if (x < 0) {
return 32
}
for (var i = 3; i >= 0; i--) { /* Assuming LE */
var v = LZ[B[i]]
if (v != 8) {
return (3 - i) * 8 - v
}
}
return 32
}
var F = new Float64Array(Buffer)
var I = new Uint32Array(Buffer)
function fpu(x) { /* Assume LE */
F[0] = x | 1
return 1054 - ((I[1] >> 20))
}
var pos = [0, 1, 28, 2, 29, 14, 24, 3,
30, 22, 20, 15, 25, 17, 4, 8,
31, 27, 13, 23, 21, 19, 16, 7,
26, 12, 18, 6, 11, 5, 10, 9]
function lookup2(v) {
v |= v >> 1
v |= v >> 2
v |= v >> 4
v |= v >> 8
v |= v >> 16
v = (v >> 1) + 1
return pos[(v * 0x077CB531) / 134217728]
}
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
lead |
|
pending… |
lead2 |
|
pending… |
lead3 |
|
pending… |
clz |
|
pending… |
clz2 |
|
pending… |
lookup |
|
pending… |
fpu |
|
pending… |
lookup2 |
|
pending… |
clz3 |
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:
- Revision 1: published by Devon Govett
- Revision 2: published by Mathieu 'p01' Henri
- Revision 3: published by Devon Govett
- Revision 4: published by Devon Govett
- Revision 5: published by Devon Govett
- Revision 6: published
- Revision 7: published by Devon Govett
- Revision 8: published
- Revision 12: published and last updated
- Revision 13: published
- Revision 14: published
0 comments