process.hrtime.bigint()方法以納秒為單位實時返回當前的high-resolution,作為NodeJS中的bigint。它不支持附加的時間參數,因為可以通過將兩個bigints變量相減來直接計算差值。
通過減去開始時間和結束時間,我們用它來計算過程之間的總時間(以納秒為單位)。
用法:
process.hrtime.bigint();
參數:此函數不帶任何參數。
返回類型:它返回bigint。
範例1:使用以下代碼創建一個index.js文件。
index.js
// Create a variable and call the
// process.hrtime() function
var start_time = process.hrtime.bigint();
// Print the Start time
console.log("Start Time:", start_time);
// Make the add function
setTimeout(function () {
console.log("Execution time will be calculated....");
// Create a variable and call the second
// process.hrtime() function
var end_time = process.hrtime.bigint();
// Print the Excution time
console.log("End Time:", end_time - start_time);
}, 2000);
使用以下命令運行index.js文件:
node index.js
輸出:
Start Time:507990695929600n Execution time will be calculated.... End Time:1005191900n
範例2:使用以下代碼創建一個index.js文件。
index.js
Javascript
// Create a variable and call the
// process.hrtime() function
var start_time = process.hrtime.bigint();
// Print the Start time
console.log("Start Time:", start_time);
// Make the add function
setTimeout(function () {
// Create two variable
var a = '45',
b = '40';
// Print the Subtraction result
console.log("Subtraction of two number is:",
(a - 0) - (b - 0));
// Create a variable and call the
// second process.hrtime() function
var end_time = process.hrtime.bigint();
// Print the Execution time
console.log("End Time:", end_time - start_time);
}, 1000);
輸出:
Start Time:507818309465700n Subtraction of two number is:5 End Time:1008706600n
參考: https://nodejs.org/api/process.html#process_process_hrtime_bigint
相關用法
- Node.js console.timeLog()用法及代碼示例
- Node.js fs.fsyncSync()用法及代碼示例
- Node.js process.nextTick()用法及代碼示例
- Node.js x509.toLegacyObject()用法及代碼示例
- Node.js GM drawLine()用法及代碼示例
- Node.js GM drawArc()用法及代碼示例
- Node.js GM drawPolyline()用法及代碼示例
- Node.js GM drawBezier()用法及代碼示例
- Node.js GM drawCircle()用法及代碼示例
- Node.js GM drawEllipse()用法及代碼示例
- Node.js GM drawPolygon()用法及代碼示例
- Node.js GM drawRectangle()用法及代碼示例
- Node.js GM paint()用法及代碼示例
注:本文由純淨天空篩選整理自_sh_pallavi大神的英文原創作品 Node.js process.hrtime.bigint() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。