Last active
February 1, 2023 21:05
-
-
Save Yaffle/c1db4670aa31961d2ca3ca204cbe87ff to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// CPU L2 cache size | |
function ASMModule1(stdlib, foreign, heap) { | |
"use asm"; | |
var memory = new stdlib.Uint32Array(heap); | |
function loop(c) { | |
c = c | 0; | |
var k = 0; | |
var j = 0; | |
for (j = c; (j | 0) != 0; j = (j - 1) | 0) { | |
k = memory[k >> 2] << 2; | |
} | |
return 0; | |
} | |
return {loop: loop}; | |
} | |
function findCacheSize() { | |
"use strict"; | |
function shuffle(array, n) { | |
// https://en.wikipedia.org/wiki/Fisher–Yates_shuffle#Modern_method | |
for (let i = n - 1; i >= 1; i--) { | |
const j = Math.floor(Math.random() * (i + 1)); | |
const e = array[j]; | |
array[j] = array[i]; | |
array[i] = e; | |
} | |
} | |
let base = 0; | |
const m = 19; | |
const heap = new ArrayBuffer(2**m * 4); | |
const memory = new Uint32Array(heap); | |
const loop = ASMModule1(globalThis, null, heap).loop; | |
let result = 0; | |
for (let e = 13; e <= m; e += 1) { | |
const n = 2**e; | |
for (let i = 0; i < n; i++) { | |
memory[i] = i; | |
} | |
shuffle(memory, n); | |
const start = performance.now(); | |
const c = 2**m * 4; | |
loop(c); | |
const end = performance.now(); | |
const f = (end - start) / (c) / base; | |
if (base === 0) { | |
base = (end - start) / (c); | |
} else { | |
console.log(n * 4, (f).toFixed(3)); | |
} | |
if (f < 4) { | |
result = n * 4; | |
} | |
} | |
return result; | |
} | |
setTimeout(() => { | |
console.time(); | |
console.log(findCacheSize()); | |
console.timeEnd(); | |
}, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment