Skip to content

Instantly share code, notes, and snippets.

@JobLeonard
Last active February 26, 2024 10:31
Show Gist options
  • Save JobLeonard/3faa2eb84da523890923549c3b316f2e to your computer and use it in GitHub Desktop.
Save JobLeonard/3faa2eb84da523890923549c3b316f2e to your computer and use it in GitHub Desktop.
Verifying alexharri.com/blog/bit-set-iteration benchmarks - 25% randomly set bits, lower half, TypedArray

Verifying alexharri.com/blog/bit-set-iteration benchmarks - 25% randomly set bits, lower half, TypedArray

view on jsbenchit

{"title":"Verifying alexharri.com/blog/bit-set-iteration benchmarks - 25% randomly set bits, lower half, TypedArray","initialization":"// Veryfying the nrs in https://alexharri.com/blog/bit-set-iteration\nconst WORD_LEN = 32;\nconst WORD_LOG = Math.log2(WORD_LEN);\n\nconst randomInit = () => (Math.random() * 0x100000000 >>> 16);\n\n// Typed Array Versions\nclass BitSetBasicTA {\n constructor(size) {\n this.words = new Uint32Array(size);\n for (let i = 0; i < size; i++) {\n this.words[i] = randomInit();\n }\n }\n \n forEach(callback) {\n const words = this.words;\n\n for (let wordIndex = 0; wordIndex < words.length; wordIndex++) {\n const word = words[wordIndex];\n\n for (let i = 0; i < WORD_LEN; i++) {\n const bitIsSetToOne = (word & (1 << i)) !== 0;\n if (bitIsSetToOne) {\n const index = (wordIndex << WORD_LOG) + i;\n callback(index);\n }\n }\n }\n }\n}\n\n// Returns the number of non-zero bits in `n`\nfunction hammingWeight(n) {\n n -= (n >> 1) & 0x55555555;\n n = (n & 0x33333333) + ((n >>> 2) & 0x33333333);\n return (((n + (n >>> 4)) & 0xf0f0f0f) * 0x1010101) >> 24;\n}\n\nclass BitSetOptimizedTA {\n constructor(size) {\n this.words = new Uint32Array(size);\n for (let i = 0; i < size; i++) {\n this.words[i] = randomInit()\n }\n }\n \n forEach(callback) {\n const words = this.words;\n for (let wordIndex = 0; wordIndex < words.length; wordIndex++) {\n let word = words[wordIndex];\n while (word !== 0) {\n const lsb = word & -word;\n const index = (wordIndex << WORD_LOG) + hammingWeight(lsb - 1);\n callback(index);\n word ^= lsb;\n }\n }\n }\n}\n\n\nclass BitSetCLZ_TA {\n constructor(size) {\n this.words = new Uint32Array(size);\n for (let i = 0; i < size; i++) {\n this.words[i] = randomInit()\n }\n }\n \n forEach(callback) {\n const words = this.words;\n const clz32 = Math.clz32;\n for (let wordIndex = 0; wordIndex < words.length; wordIndex++) {\n let word = words[wordIndex];\n while (word !== 0) {\n const index = 31 - clz32(word);\n callback(index);\n word ^= (1 << index);\n }\n }\n }\n}\n\nconst size = 10000;\nconst basic = new BitSetBasicTA(size);\nconst optimized = new BitSetOptimizedTA(size);\nconst clz = new BitSetCLZ_TA(size);","setup":"// runs before each test","tests":[{"name":"basic.forEach(() => {})","code":"basic.forEach(() => {});","results":{"aborted":false,"count":43,"cycles":2,"hz":468.3987848245916,"stats":{"moe":0.00003696207189056763,"rme":1.7312989558141072,"sem":0.00001885819994416716,"deviation":0.00014112184624470598,"mean":0.0021349329511486355,"variance":1.9915375487514432e-8,"numSamples":56},"times":{"cycle":0.09180211689939133,"elapsed":5.928,"period":0.0021349329511486355,"timeStamp":1708943433929}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0":{"aborted":false,"count":43,"cycles":2,"hz":468.3987848245916,"stats":{"moe":0.00003696207189056763,"rme":1.7312989558141072,"sem":0.00001885819994416716,"deviation":0.00014112184624470598,"mean":0.0021349329511486355,"variance":1.9915375487514432e-8,"numSamples":56},"times":{"cycle":0.09180211689939133,"elapsed":5.928,"period":0.0021349329511486355,"timeStamp":1708943433929}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36":{"aborted":false,"count":51,"cycles":4,"hz":664.0108822386321,"stats":{"moe":0.000010726475052685566,"rme":0.7122496163044421,"sem":0.000005472691353411003,"deviation":0.000044795909339245146,"mean":0.0015059994146912493,"variance":2.0066734935298705e-9,"numSamples":67},"times":{"cycle":0.07680597014925371,"elapsed":5.905,"period":0.0015059994146912493,"timeStamp":1708943381216}}}},{"name":"optimized.forEach(() => {})","code":"optimized.forEach(() => {});","results":{"aborted":false,"count":102,"cycles":4,"hz":670.1406479940171,"stats":{"moe":0.0004071705086288612,"rme":27.286150849659858,"sem":0.00020774005542288838,"deviation":0.0012805957066264043,"mean":0.0014922240622075023,"variance":0.00000163992536382998,"numSamples":38},"times":{"cycle":0.15220685434516523,"elapsed":5.756,"period":0.0014922240622075023,"timeStamp":1708943439863}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0":{"aborted":false,"count":102,"cycles":4,"hz":670.1406479940171,"stats":{"moe":0.0004071705086288612,"rme":27.286150849659858,"sem":0.00020774005542288838,"deviation":0.0012805957066264043,"mean":0.0014922240622075023,"variance":0.00000163992536382998,"numSamples":38},"times":{"cycle":0.15220685434516523,"elapsed":5.756,"period":0.0014922240622075023,"timeStamp":1708943439863}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36":{"aborted":false,"count":103,"cycles":8,"hz":1331.213962256223,"stats":{"moe":0.000006418985692387439,"rme":0.8545043377229089,"sem":0.000003274992700197673,"deviation":0.000026806970576425064,"mean":0.0007511940441979279,"variance":7.186136714853192e-10,"numSamples":67},"times":{"cycle":0.07737298655238657,"elapsed":6.232,"period":0.0007511940441979279,"timeStamp":1708943387126}}}},{"name":"clz.forEach(() => {})","code":"clz.forEach(() => {});","results":{"aborted":false,"count":976,"cycles":2,"hz":1654.884570236077,"stats":{"moe":0.000040523096475933096,"rme":6.706104709620962,"sem":0.000018597107148202432,"deviation":0.00006705282339814175,"mean":0.0006042717528373265,"variance":4.496081125662386e-9,"numSamples":13},"times":{"cycle":0.5897692307692307,"elapsed":7.828,"period":0.0006042717528373265,"timeStamp":1708943445626}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0":{"aborted":false,"count":976,"cycles":2,"hz":1654.884570236077,"stats":{"moe":0.000040523096475933096,"rme":6.706104709620962,"sem":0.000018597107148202432,"deviation":0.00006705282339814175,"mean":0.0006042717528373265,"variance":4.496081125662386e-9,"numSamples":13},"times":{"cycle":0.5897692307692307,"elapsed":7.828,"period":0.0006042717528373265,"timeStamp":1708943445626}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36":{"aborted":false,"count":121,"cycles":3,"hz":1570.162883190805,"stats":{"moe":0.000004289757915863227,"rme":0.6735618657362383,"sem":0.0000021886519978894017,"deviation":0.00001791488869758819,"mean":0.0006368766009599279,"variance":3.2094323704697306e-10,"numSamples":67},"times":{"cycle":0.07706206871615127,"elapsed":5.894,"period":0.0006368766009599279,"timeStamp":1708943393364}}}},{"name":"basic.forEach(value => sum += value)","code":"let sum = 0;\nbasic.forEach(value => sum += value);","results":{"aborted":false,"count":34,"cycles":3,"hz":403.0052614369371,"stats":{"moe":0.00002307402519967707,"rme":0.9298953557998332,"sem":0.000011772461836569935,"deviation":0.00008965639824475855,"mean":0.002481357182371381,"variance":8.038269746222744e-9,"numSamples":58},"times":{"cycle":0.08436614420062695,"elapsed":5.787,"period":0.002481357182371381,"timeStamp":1708943453459}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0":{"aborted":false,"count":34,"cycles":3,"hz":403.0052614369371,"stats":{"moe":0.00002307402519967707,"rme":0.9298953557998332,"sem":0.000011772461836569935,"deviation":0.00008965639824475855,"mean":0.002481357182371381,"variance":8.038269746222744e-9,"numSamples":58},"times":{"cycle":0.08436614420062695,"elapsed":5.787,"period":0.002481357182371381,"timeStamp":1708943453459}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36":{"aborted":false,"count":41,"cycles":2,"hz":524.1512896319704,"stats":{"moe":0.000012544141206445707,"rme":0.6575027790684057,"sem":0.000006400072044104952,"deviation":0.00005199443107874561,"mean":0.0019078461119539437,"variance":2.7034208632024267e-9,"numSamples":66},"times":{"cycle":0.07822169059011169,"elapsed":5.844,"period":0.0019078461119539437,"timeStamp":1708943399263}}}},{"name":"optimized.forEach(value => sum += value)","code":"let sum = 0;\noptimized.forEach(value => sum += value);","results":{"aborted":false,"count":72,"cycles":3,"hz":474.1135487534535,"stats":{"moe":0.0005117925106356678,"rme":24.264776344291604,"sem":0.0002611186278753407,"deviation":0.0015667117672520442,"mean":0.0021091993735028562,"variance":0.0000024545857616460237,"numSamples":36},"times":{"cycle":0.15186235489220565,"elapsed":5.965,"period":0.0021091993735028562,"timeStamp":1708943459251}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0":{"aborted":false,"count":72,"cycles":3,"hz":474.1135487534535,"stats":{"moe":0.0005117925106356678,"rme":24.264776344291604,"sem":0.0002611186278753407,"deviation":0.0015667117672520442,"mean":0.0021091993735028562,"variance":0.0000024545857616460237,"numSamples":36},"times":{"cycle":0.15186235489220565,"elapsed":5.965,"period":0.0021091993735028562,"timeStamp":1708943459251}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36":{"aborted":false,"count":68,"cycles":3,"hz":867.7488313357309,"stats":{"moe":0.000007441024703609132,"rme":0.6456940490497127,"sem":0.0000037964411753107816,"deviation":0.000030607887281609,"mean":0.0011524071988212236,"variance":9.36842763843682e-10,"numSamples":65},"times":{"cycle":0.07836368951984321,"elapsed":5.876,"period":0.0011524071988212236,"timeStamp":1708943405113}}}},{"name":"clz.forEach(value => sum += value)","code":"let sum = 0;\nclz.forEach(value => sum += value);","results":{"aborted":false,"count":127,"cycles":3,"hz":1306.4177084558437,"stats":{"moe":0.000018079193766361078,"rme":2.3618978890978615,"sem":0.00000922407845222504,"deviation":0.00006778285666605771,"mean":0.0007654519634321074,"variance":4.594515657811324e-9,"numSamples":54},"times":{"cycle":0.09721239935587764,"elapsed":5.932,"period":0.0007654519634321074,"timeStamp":1708943465222}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0":{"aborted":false,"count":127,"cycles":3,"hz":1306.4177084558437,"stats":{"moe":0.000018079193766361078,"rme":2.3618978890978615,"sem":0.00000922407845222504,"deviation":0.00006778285666605771,"mean":0.0007654519634321074,"variance":4.594515657811324e-9,"numSamples":54},"times":{"cycle":0.09721239935587764,"elapsed":5.932,"period":0.0007654519634321074,"timeStamp":1708943465222}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36":{"aborted":false,"count":89,"cycles":4,"hz":1121.3932577593696,"stats":{"moe":0.000008080744832243624,"rme":0.9061692772551868,"sem":0.000004122828996042665,"deviation":0.00003323931001825491,"mean":0.0008917478262693298,"variance":1.1048517304896612e-9,"numSamples":65},"times":{"cycle":0.07936555653797035,"elapsed":5.97,"period":0.0008917478262693298,"timeStamp":1708943410995}}}}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment