I found several articles on the Internet and questions here about how to measure the code performance in Node. But I got results that differ by about two times, depending on the measurement tool. Target function is:
let arr = [75, 283, 361, 168, 23, 489, 421, 95, 72, 380, 384, 470, 235, 465, 276, 26, 364, 416, 373, 184, 211, 239, 485, 18, 19, 252, 447, 6, 291, 324, 497, 352,
458, 201, 238, 116, 333, 163, 207, 417, 340, 431, 5, 269, 258, 178, 182, 295, 257, 434, 37, 372, 154, 223, 313, 80, 71, 229, 379, 181, 396, 281, 491, 58, 254,
359, 79, 175, 143, 214, 217, 148, 393, 246, 34, 166, 251, 381, 413, 180, 338,
442, 494, 378, 123, 118, 395, 446, 459, 472, 457, 51, 127, 351, 389, 157, 260,
370, 405, 346]
const { performance } = require("perf_hooks")
function summBrute(arr, k) {
for(let i = 0; i < arr.length; i++) {
for(let j = i + 1; j < arr.length; j++) {
if(arr[i] + arr[j] == k) {
return true;
}
}
}
return false;
}
And measurement methods are:
console.time('summBrute')
summBrute(arr, 394)
console.timeEnd('summBrute')
var t0 = performance.now()
summBrute(arr, 394) //
var t1 = performance.now()
console.log("Call to summBrute took " + (t1 - t0) + " milliseconds.")
Here I want to find out whether there are two numbers in the array in question, add which, I get the second argument of the called function. I use these two methods independently of each other, just commenting out the corresponding section of the code. console.time()
gives an average 0.300ms performance score console.time result
and performance.now()
gives 0.170ms performance.now result
Please help me to understand why particular THESE two methods give different results (almost doubled)? I am using Node v15, CPU Core i5, 8GB RAM, Win10.
If you put the measurements in a small loop, you will see that they are barely consistent with themselves.
Produces
they are almost random numbers, useless.
However if I call
summBrute()
10 million times:The results stop depending on the mood of my computer and become much more consistent:
It still has a spread of almost half seconds, but it's pretty visible that the average (of 10 million calls) is around 16 seconds, and thus one call is around 0.016 ms, which is clearly not what any of the individual calls measured.
TL;DR: time is time, it matters far more what you measure than the timer API you pick.