简体   繁体   English

JavaScript 程序的执行时间

[英]Execution time of JavaScript programs

I am dealing with execution time of JavaScript programs.我正在处理 JavaScript 程序的执行时间。 When I run them with nodejs/v8, time command on shell returns varying execution times.当我使用 nodejs/v8 运行它们时,shell 上的 time 命令返回不同的执行时间。

*Is it possible to sandbox the execution of such program so that I will get constant or less varying execution times across different runs? *是否可以对此类程序的执行进行沙箱处理,以便我在不同的运行中获得恒定或变化较小的执行时间?

*Is there any other way to check the execution time of these programs using nodejs/v8? *有没有其他方法可以使用 nodejs/v8 检查这些程序的执行时间?

Have a look at node's own process.hrtime() .看看节点自己的process.hrtime()

This returns high-resolution real time in a [seconds, nanoseconds] tuple Array.这将在 [seconds, nanoseconds] 元组数组中返回高分辨率实时。 It is relative to an arbitrary time in the past.它相对于过去的任意时间。 It is not related to the time of day and therefore not subject to clock drift.它与一天中的时间无关,因此不受时钟漂移的影响。 The primary use is for measuring performance between intervals.主要用途是测量间隔之间的性能。 You may pass in the result of a previous call to process.hrtime() to get a diff reading, useful for benchmarks and measuring intervals您可以传入之前调用 process.hrtime() 的结果以获得差异读数,这对基准测试和测量间隔很有用

var time = process.hrtime();
// [ 1800216, 25 ]

setTimeout(function() {
  var diff = process.hrtime(time);
  // [ 1, 552 ]

  console.log('benchmark took %d nanoseconds', diff[0] * 1e9 + diff[1]);
  // benchmark took 1000000527 nanoseconds
}, 1000);

Have a read of this useful blog post阅读这篇有用的博客文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM