-
-
Save kripken/5555c7ea0bac10c6b1a65bbac25a6dbb to your computer and use it in GitHub Desktop.
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
<html> | |
<body> | |
<script> | |
function sum(array) { | |
var num = 0; | |
for (var i = 0, l = array.length; i < l; i++) num += array[i]; | |
return num; | |
} | |
function mean(array) { | |
return sum(array) / array.length; | |
} | |
function variance(array) { | |
var m = mean(array); | |
return mean(array.map(function (num) { | |
return Math.pow(num - m, 2); | |
})); | |
} | |
function standardDeviation(array) { | |
return Math.sqrt(variance(array)); | |
} | |
window.onload = function () { | |
run(20); | |
} | |
let sab = new SharedArrayBuffer(4); | |
let arr = new Uint32Array(sab); | |
function measureTicksPerMs(c) { | |
let s = performance.now(); | |
let a = arr; | |
for (let i = 0; i < c; i++) { | |
a[0]++; | |
} | |
return (c / (performance.now() - s)) | 0; | |
} | |
function createWorker() { | |
var code = ` | |
addEventListener("message", function (message) { | |
let arr = new Uint32Array(message.data); | |
while (true) { | |
arr[0] ++; | |
} | |
}); | |
`; | |
var blob = new Blob([code], {type: "application/javascript"}); | |
return new Worker(URL.createObjectURL(blob)); | |
} | |
function run(k) { | |
var worker = createWorker(); | |
worker.postMessage(sab); | |
let ticksPerMs = measureTicksPerMs(10000000); | |
function now() { | |
return arr[0] / ticksPerMs; | |
} | |
// Something to do that takes a while. | |
function gcd(a, b) { | |
if (!b) { | |
return a; | |
} | |
return gcd(b, a % b); | |
} | |
function run() { | |
arr[0] = 0; // do this here so VM can't eliminate the writes to memory from before | |
function benchmark() { | |
let A = []; | |
let B = []; | |
// Should I recompute this periodically? | |
// ticksPerMs = measureTicksPerMs(10000000); | |
// run k iterations, and average out | |
let k = 200; | |
let s0 = performance.now(); | |
let s1 = now(); | |
function one() { | |
if (k-- == 0) { | |
console.log(""); | |
console.log(" ticksPerMs: " + ticksPerMs); | |
console.log(" performance.now: " + mean(A)); | |
console.log(" sab.now: " + mean(B)); | |
console.log("performance.now std: " + standardDeviation(A)); | |
console.log(" sab.now std: " + standardDeviation(B)); | |
setTimeout(benchmark, 1); | |
return; | |
} | |
let e0 = (performance.now() - s0); | |
let e1 = (now() - s1); | |
A.push(e0); | |
B.push(e1); | |
s0 = e0; | |
s1 = e1; | |
setTimeout(one, 10); | |
} | |
setTimeout(one, 10); | |
} | |
benchmark(); | |
} | |
setTimeout(run, 100); // Wait a little while for the worker thread to start. | |
} | |
</script> | |
Open up the console. I'm trying to see if I can build a more accurate timer using SABs. | |
At the moment I'm getting a larger std for the SAB case. | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment