Skip to content

Instantly share code, notes, and snippets.

@JobLeonard
Last active February 26, 2024 10:43
Show Gist options
  • Save JobLeonard/646bf8088d0cd2808e236518b7eaa944 to your computer and use it in GitHub Desktop.
Save JobLeonard/646bf8088d0cd2808e236518b7eaa944 to your computer and use it in GitHub Desktop.
Verifying alexharri.com/blog/bit-set-iteration benchmarks - 3.125% randomly set bits, TypedArray

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

view on jsbenchit

{"title":"Verifying alexharri.com/blog/bit-set-iteration benchmarks - 3.125% randomly set bits, 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 = () => (1 << (Math.random() * 32)) >>> 0;\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":69,"cycles":6,"hz":862.6688097172711,"stats":{"moe":0.000013878297590742994,"rme":1.197237446350833,"sem":0.000007080764076909691,"deviation":0.00005575399209560688,"mean":0.001159193410884691,"variance":3.1085076345969947e-9,"numSamples":62},"times":{"cycle":0.07998434535104368,"elapsed":6.103,"period":0.001159193410884691,"timeStamp":1708944182463}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0":{"aborted":false,"count":69,"cycles":6,"hz":862.6688097172711,"stats":{"moe":0.000013878297590742994,"rme":1.197237446350833,"sem":0.000007080764076909691,"deviation":0.00005575399209560688,"mean":0.001159193410884691,"variance":3.1085076345969947e-9,"numSamples":62},"times":{"cycle":0.07998434535104368,"elapsed":6.103,"period":0.001159193410884691,"timeStamp":1708944182463}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36":{"aborted":false,"count":162,"cycles":4,"hz":1902.939371647196,"stats":{"moe":0.000003664451208115248,"rme":0.6973228479402638,"sem":0.0000018696179633241063,"deviation":0.00001460218309212136,"mean":0.0005255028167998826,"variance":2.1322375105583491e-10,"numSamples":61},"times":{"cycle":0.08513145632158098,"elapsed":5.924,"period":0.0005255028167998826,"timeStamp":1708943780196}}}},{"name":"optimized.forEach(() => {})","code":"optimized.forEach(() => {});","results":{"aborted":false,"count":976,"cycles":2,"hz":5999.638401735671,"stats":{"moe":0.000004902017543483771,"rme":2.9410332699867188,"sem":0.000002501029358920291,"deviation":0.000014583381879459997,"mean":0.0001666767116682739,"variance":2.1267502704216219e-10,"numSamples":34},"times":{"cycle":0.1626764705882353,"elapsed":5.966,"period":0.0001666767116682739,"timeStamp":1708944188571}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0":{"aborted":false,"count":976,"cycles":2,"hz":5999.638401735671,"stats":{"moe":0.000004902017543483771,"rme":2.9410332699867188,"sem":0.000002501029358920291,"deviation":0.000014583381879459997,"mean":0.0001666767116682739,"variance":2.1267502704216219e-10,"numSamples":34},"times":{"cycle":0.1626764705882353,"elapsed":5.966,"period":0.0001666767116682739,"timeStamp":1708944188571}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36":{"aborted":false,"count":700,"cycles":5,"hz":8950.679210069553,"stats":{"moe":0.000001067784378300435,"rme":0.9557395435690745,"sem":5.447879481124669e-7,"deviation":0.0000044592815411650855,"mean":0.00011172336495704099,"variance":1.988519186337566e-11,"numSamples":67},"times":{"cycle":0.0782063554699287,"elapsed":6.128,"period":0.00011172336495704099,"timeStamp":1708943786125}}}},{"name":"clz.forEach(() => {})","code":"clz.forEach(() => {});","results":{"aborted":false,"count":976,"cycles":2,"hz":8480.544012089156,"stats":{"moe":0.000003115982018921106,"rme":2.6425222652338864,"sem":0.000001589786744347503,"deviation":0.000010782458302963176,"mean":0.0001179169636493229,"variance":1.1626140705513952e-10,"numSamples":46},"times":{"cycle":0.11508695652173914,"elapsed":5.836,"period":0.0001179169636493229,"timeStamp":1708944194543}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0":{"aborted":false,"count":976,"cycles":2,"hz":8480.544012089156,"stats":{"moe":0.000003115982018921106,"rme":2.6425222652338864,"sem":0.000001589786744347503,"deviation":0.000010782458302963176,"mean":0.0001179169636493229,"variance":1.1626140705513952e-10,"numSamples":46},"times":{"cycle":0.11508695652173914,"elapsed":5.836,"period":0.0001179169636493229,"timeStamp":1708944194543}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36":{"aborted":false,"count":796,"cycles":7,"hz":10280.721747388417,"stats":{"moe":0.0000010714305294862515,"rme":1.101507914530519,"sem":5.466482293297201e-7,"deviation":0.000004507776779166604,"mean":0.00009726943541235586,"variance":2.0320051490793643e-11,"numSamples":68},"times":{"cycle":0.07742647058823526,"elapsed":6.101,"period":0.00009726943541235586,"timeStamp":1708943792259}}}},{"name":"basic.forEach(value => sum += value)","code":"let sum = 0;\nbasic.forEach(value => sum += value);","results":{"aborted":false,"count":68,"cycles":3,"hz":846.2691529692789,"stats":{"moe":0.000020860884619745542,"rme":1.765392315734192,"sem":0.000010643308479462011,"deviation":0.0000824427129790091,"mean":0.0011816571553994735,"variance":6.7968009233392755e-9,"numSamples":60},"times":{"cycle":0.0803526865671642,"elapsed":5.893,"period":0.0011816571553994735,"timeStamp":1708944200385}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0":{"aborted":false,"count":68,"cycles":3,"hz":846.2691529692789,"stats":{"moe":0.000020860884619745542,"rme":1.765392315734192,"sem":0.000010643308479462011,"deviation":0.0000824427129790091,"mean":0.0011816571553994735,"variance":6.7968009233392755e-9,"numSamples":60},"times":{"cycle":0.0803526865671642,"elapsed":5.893,"period":0.0011816571553994735,"timeStamp":1708944200385}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36":{"aborted":false,"count":143,"cycles":3,"hz":1692.3866321851524,"stats":{"moe":0.000004997801657089325,"rme":0.8458212714770776,"sem":0.0000025498988046374108,"deviation":0.000019751431209749783,"mean":0.0005908815284772332,"variance":3.901190348334777e-10,"numSamples":60},"times":{"cycle":0.08449605857224435,"elapsed":5.886,"period":0.0005908815284772332,"timeStamp":1708943798365}}}},{"name":"optimized.forEach(value => sum += value)","code":"let sum = 0;\noptimized.forEach(value => sum += value);","results":{"aborted":false,"count":458,"cycles":3,"hz":3298.2196832718114,"stats":{"moe":0.00006546260147051038,"rme":21.591004068821555,"sem":0.00003339928646454611,"deviation":0.00020857847711902518,"mean":0.00030319387306791126,"variance":4.350498111729171e-8,"numSamples":39},"times":{"cycle":0.13886279386510336,"elapsed":5.855,"period":0.00030319387306791126,"timeStamp":1708944206283}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0":{"aborted":false,"count":458,"cycles":3,"hz":3298.2196832718114,"stats":{"moe":0.00006546260147051038,"rme":21.591004068821555,"sem":0.00003339928646454611,"deviation":0.00020857847711902518,"mean":0.00030319387306791126,"variance":4.350498111729171e-8,"numSamples":39},"times":{"cycle":0.13886279386510336,"elapsed":5.855,"period":0.00030319387306791126,"timeStamp":1708944206283}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36":{"aborted":false,"count":489,"cycles":5,"hz":6124.243767176438,"stats":{"moe":0.0000030122437515906858,"rme":1.8447715020895428,"sem":0.0000015368590569340234,"deviation":0.000012390553839809131,"mean":0.00016328546642111318,"variance":1.535258244572088e-10,"numSamples":65},"times":{"cycle":0.07984659307992434,"elapsed":6.086,"period":0.00016328546642111318,"timeStamp":1708943804256}}}},{"name":"clz.forEach(value => sum += value)","code":"let sum = 0;\nclz.forEach(value => sum += value);","results":{"aborted":false,"count":616,"cycles":4,"hz":7047.291582660186,"stats":{"moe":0.0000025675741250218863,"rme":1.809444351912283,"sem":0.0000013099867984805543,"deviation":0.00000997656222890497,"mean":0.00014189848515144363,"variance":9.95317939072133e-11,"numSamples":58},"times":{"cycle":0.08740946685328928,"elapsed":5.83,"period":0.00014189848515144363,"timeStamp":1708944212143}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0":{"aborted":false,"count":616,"cycles":4,"hz":7047.291582660186,"stats":{"moe":0.0000025675741250218863,"rme":1.809444351912283,"sem":0.0000013099867984805543,"deviation":0.00000997656222890497,"mean":0.00014189848515144363,"variance":9.95317939072133e-11,"numSamples":58},"times":{"cycle":0.08740946685328928,"elapsed":5.83,"period":0.00014189848515144363,"timeStamp":1708944212143}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36":{"aborted":false,"count":976,"cycles":2,"hz":7906.390639063909,"stats":{"moe":0.0000013682254574153725,"rme":1.0817724948637835,"sem":6.980742129670268e-7,"deviation":0.000004682824180601811,"mean":0.00012647996357012748,"variance":2.1928842306429025e-11,"numSamples":45},"times":{"cycle":0.12344444444444441,"elapsed":5.893,"period":0.00012647996357012748,"timeStamp":1708943810348}}}}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment