Node.js 中的 console.time() 与 performance.now() [英] console.time() vs performance.now() in Node.js

查看:22
本文介绍了Node.js 中的 console.time() 与 performance.now()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上找了几篇文章,这里有关于如何在 Node.js 中衡量代码性能的问题.但是我得到的结果大约相差两倍,这取决于测量工具.目标函数是:

let arr = [75, 283, 361, 168, 23, 489, 421, 95, 72, 380, 384, 470, 235, 465, 276, 26, 364, 416, 14 3211, 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, 37, 37, 3, 3, 7229, 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 { 性能 } = 要求(perf_hooks")函数 summBrute(arr, k) {for(let i = 0; i < arr.length; i++) {for(let j = i + 1; j 

和测量方法是:

console.time('summBrute')summBrute(arr,394)console.timeEnd('summBrute')var t0 = performance.now()summBrute(arr, 394)//var t1 = performance.now()console.log(调用 summBrute 耗时+(t1 - t0) +毫秒".)

这里我想找出有问题的数组中是否有两个数字,添加哪个,我得到被调用函数的第二个参数.这两种方法我是相互独立使用的,只是注释掉了相应的代码部分.console.time() 给出平均 0.300 毫秒的性能得分 console.time结果

performance.now() 给出 0.170ms performance.now结果

请帮助我理解为什么这两种方法会给出不同的结果(几乎翻了一番)?我使用的是 Node v15、CPU Core i5、8GB RAM、Win10.

解决方案

如果您将测量值放在一个小循环中,您会发现它们与自身几乎不一致.

for(let i=0;i<5;i++){console.time('summBrute')summBrute(arr,394)console.timeEnd('summBrute')var t0 = performance.now()summBrute(arr, 394)//var t1 = performance.now()console.log(调用 summBrute 耗时+(t1 - t0) +毫秒".)}

生产

<块引用>

summBrute: 0.234ms调用 summBrute 耗时 0.1938999891281128 毫秒.summBrute:0.171ms调用 summBrute 耗时 0.13520002365112305 毫秒.summBrute:0.107ms调用 summBrute 耗时 0.1332000494003296 毫秒.summBrute:0.095ms调用 summBrute 耗时 0.10259997844696045 毫秒.summBrute:1.385ms调用 summBrute 耗时 0.10839998722076416 毫秒.

它们几乎是随机数,没用.
但是,如果我调用 summBrute() 1000 万次:

for(let i=0;i<5;i++){console.time('summBrute')for(让 j=0;j<10000000;j++)summBrute(arr,394)console.timeEnd('summBrute')var t0 = performance.now()for(让 j=0;j<10000000;j++)summBrute(arr, 394)//var t1 = performance.now()console.log(调用 summBrute 耗时+(t1 - t0) +毫秒".)}

结果不再取决于我的计算机的情绪,而是变得更加一致:

<块引用>

summBrute: 16.369s调用 summBrute 耗时 16184.267899990082 毫秒.summBrute:15.643s调用 summBrute 耗时 15852.86260008812 毫秒.summBrute:16.355s调用 summBrute 耗时 15942.392500042915 毫秒.summBrute:16.190s调用 summBrute 耗时 16314.965299963951 毫秒.summBrute:16.523s调用 summBrute 耗时 16744.983800053596 毫秒.

它仍然有将近半秒的传播,但很明显平均(1000 万次调用)大约是 16 秒,因此一个调用大约是 0.016 毫秒,这显然不是任何单个调用的测量.

TL;DR:时间就是时间,您衡量的内容比您选择的计时器 API 重要得多.

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.

for(let i=0;i<5;i++){

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.")

}

Produces

summBrute: 0.234ms
Call to summBrute took 0.1938999891281128 milliseconds.
summBrute: 0.171ms
Call to summBrute took 0.13520002365112305 milliseconds.
summBrute: 0.107ms
Call to summBrute took 0.1332000494003296 milliseconds.
summBrute: 0.095ms
Call to summBrute took 0.10259997844696045 milliseconds.
summBrute: 1.385ms
Call to summBrute took 0.10839998722076416 milliseconds.

they are almost random numbers, useless.
However if I call summBrute() 10 million times:

for(let i=0;i<5;i++){

console.time('summBrute')
for(let j=0;j<10000000;j++)
  summBrute(arr, 394)
console.timeEnd('summBrute')

var t0 = performance.now()
for(let j=0;j<10000000;j++)
  summBrute(arr, 394)  //
var t1 = performance.now()
console.log("Call to summBrute took " + (t1 - t0) + " milliseconds.")

}

The results stop depending on the mood of my computer and become much more consistent:

summBrute: 16.369s
Call to summBrute took 16184.267899990082 milliseconds.
summBrute: 15.643s
Call to summBrute took 15852.86260008812 milliseconds.
summBrute: 16.355s
Call to summBrute took 15942.392500042915 milliseconds.
summBrute: 16.190s
Call to summBrute took 16314.965299963951 milliseconds.
summBrute: 16.523s
Call to summBrute took 16744.983800053596 milliseconds.

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.

这篇关于Node.js 中的 console.time() 与 performance.now()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆