当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Node.js process.hrtime.bigint()用法及代码示例

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

相关用法


注:本文由纯净天空筛选整理自_sh_pallavi大神的英文原创作品 Node.js process.hrtime.bigint() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。