Skip to content

Instantly share code, notes, and snippets.

@halemmerich
Last active Jun 16, 2022
Embed
What would you like to do?
var count = 0;
var time = 100;
var tries = 10;
var lastTimeTimeout = [];
var lastTimeInterval = [];
function pad(text, length, leftaligned){
let padding = new Array(length - (text + "").length).join(" ");
return leftaligned ? text + padding : padding + text;
}
function format(name, n1, n2, n3){
console.log(pad(name, 15, true), pad(n1, 20), pad(n2, 20), pad(n3, 20));
}
function analyse(name, data) {
let max = 0;
let min = Number.MAX_VALUE;
let sum = 0;
let initialOffset = data[1] - data[0];
let max_oc = 0;
let min_oc = Number.MAX_VALUE;
let sum_oc = 0;
let max_r = 0;
let min_r = Number.MAX_VALUE;
let sum_r = 0;
for (let i = 2; i < data.length; i++) {
if (data[i] > max) {
max = data[i] - data[0] - time * (i - 1);
}
if (data[i] > max_r) {
max_r = data[i] - data[i-1] - time;
}
if (data[i] - initialOffset > max_oc) {
max_oc =data[i] - data[0] - initialOffset - time * (i - 1);
}
if (data[i] < min) {
min = data[i] - data[0] - time * (i - 1);
}
if (data[i] < min_r) {
min_r = data[i] - data[i-1] - time;
}
if (data[i] - initialOffset < min_oc) {
min_oc = data[i] - data[0] - initialOffset - time * (i - 1);
}
sum += data[i] - data[0] - time * (i - 1);
sum_r += data[i] - data[i-1] - time;
sum_oc += data[i] - data[0] - initialOffset - time * (i - 1);
}
let mean = sum / data.length - 2;
let mean_r = sum_r / data.length - 2;
let mean_oc = sum_oc / data.length - 2;
let squaredDiffSum = 0;
let squaredDiffSum_r = 0;
let squaredDiffSum_oc = 0;
for (let i = 2; i < data.length; i++) {
squaredDiffSum += Math.pow(data[i] - data[0] - time * (i - 1) - mean, 2);
squaredDiffSum_r += Math.pow(data[i] - data[i-1] - time - mean_r, 2);
squaredDiffSum_oc += Math.pow(data[i] - data[0] - initialOffset - time * (i - 1) - mean_oc, 2);
}
let sd = Math.sqrt(squaredDiffSum / (data.length - 2));
let sd_r = Math.sqrt(squaredDiffSum_r / (data.length - 2));
let sd_oc = Math.sqrt(squaredDiffSum_oc / (data.length - 2));
console.log(name);
format("InitialOffset",initialOffset,"","");
format("","direct","offset corrected", "relative");
format("Min", min, min_oc, min_r);
format("Max", max, max_oc, max_r);
format("Mean", mean, mean_oc, mean_r);
format("Sum", sum, sum_oc, sum_r);
format("SD", sd, sd_oc, sd_r);
}
var interval;
function resetTimer() {
var cTime, target;
if (count < tries) {
//ignore first timestamp, will be at the wrong time
cTime = Date.now();
target = time - cTime % time;
if (count > 0){
lastTimeTimeout.push(cTime);
} else {
lastTimeTimeout.push(target + cTime);
}
count++;
setTimeout(resetTimer, target);
} else {
count = 0;
cTime = Date.now();
target = time - cTime % time;
lastTimeInterval.push(cTime + target + time);
setTimeout(()=>{
interval = setInterval(() => {
cTime = Date.now();
if (count < tries) {
lastTimeInterval.push(cTime);
count++;
} else {
clearInterval(interval);
analyse("setTimeout", lastTimeTimeout);
console.log("-------------");
analyse("setInterval", lastTimeInterval);
}
}, time);
}, target);
}
}
resetTimer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment