我使用 process.hrtime() 来计算进程花费的时间(以秒和毫秒为单位),如下所示:
router.post(
"/api/result-store/v1/indexing-analyzer/:searchID/:id",
async (req, res) => {
var hrstart = process.hrtime();
//some code which takes time
hrend = process.hrtime(hrstart);
console.info("Execution time (hr): %ds %dms", hrend[0], hrend[1] / 1000000);
}
);
Run Code Online (Sandbox Code Playgroud)
我遵循以下代码:
https://blog.abelotech.com/posts/measure-execution-time-nodejs-javascript/
所以我希望得到以毫秒和秒为单位的匹配时间,但这是我得到的:
Execution time (hr): 54s 105.970357ms
Run Code Online (Sandbox Code Playgroud)
所以这很奇怪,因为当我将 54 秒转换为毫秒时,我得到了 54000,所以我不知道这个“105.970357ms”来自哪里。我的代码有什么问题吗?为什么我会看到这种不匹配?
根据process.hrtime()文档,它返回一个 array [seconds, nanoseconds],其中nanoseconds是无法以第二精度表示的实时的剩余部分。
1 纳秒 = 10^9 秒
1 毫秒 = 10^6 纳秒
在你的情况下,执行采取了54 seconds and 105.970357 millisecondsor
54000 milliseconds + 105.970357 milliseconds。
或者如果您在几秒钟内需要它:(hrend[0]+ hrend[1] / Math.pow(10,9))
| 归档时间: |
|
| 查看次数: |
2368 次 |
| 最近记录: |