简体   繁体   English

如何衡量脚本的执行时间?

[英]How can I measure the execution time of a script?

How would I measure the time it takes for a script to run from start to finish? 我如何衡量脚本从头到尾运行所需的时间?

 start-timing
   //CODE
 end-timing

EDIT : in January 2011, this was the best available solution. 编辑 :2011年1月,这是最好的解决方案。 Other solutions (such as performance.now() should be preferred now. 现在应该首选其他解决方案(例如performance.now()

var start = new Date();
    // CODE
var time = new Date() - start;
// time is the number of milliseconds it taken to execute the script

You may also want to wrap that in a function: 您可能还想将其包装在函数中:

function time_my_script(script) {
    var start = new Date();
    script();
    return new Date() - start;
}

// call it like this:
time = time_my_script(function() {
    // CODE
});

// or just like this:
time = time_my_script(func);

If you are trying to profile your code, you may want to try the Firebug extension, which includes a javascript profiler. 如果您尝试配置代码,可能需要尝试Firebug扩展,其中包括一个javascript探查器。 It has a great user interface for profiling, but it also can be done programmatically with its console api : 它有一个很好的用户界面进行性能分析,但它也可以通过其控制台api以编程方式完成:

console.time('timer1');
    // CODE
console.timeEnd('timer1'); // this prints times on the console

console.profile('profile1');
    // CODE
console.profileEnd('profile1'); // this prints usual profiling informations, per function, etc.

Use performance.now() instead of new Date() . 使用performance.now()而不是new Date() This provides a more accurate and better result. 这提供了更准确和更好的结果。 See this answer https://stackoverflow.com/a/15641427/730000 请参阅此答案https://stackoverflow.com/a/15641427/730000

Here is a quick function to work like a stopwatch 这是一个像秒表一样工作的快速功能

var Timer = function(id){
  var self = this;
  self.id  = id;
  var _times = [];
  self.start = function(){
    var time = performance.now();
    console.log('[' + id + '] Start');
    _times.push(time);
  }
  self.lap = function(time){
    time = time ? time: performance.now();
    console.log('[' + id + '] Lap ' + time - _times[_times.length - 1]);
    _times.push(time);
  }
  self.stop = function(){
    var time = performance.now();
    if(_times.length > 1){
      self.lap(time);
    }
    console.log('[' + id + '] Stop ' + (time - _times[0]));
    _times = [];
  }
}
// called with
var timer = new Timer('process label');
timer.start(); // logs => '[process label] Start'
// ... code ...
timer.lap();   // logs => '[process label] Lap ' + lap_time
// ... code ...
timer.stop();  // logs => '[process label] Stop ' + start_stop_diff

Use the User Timing api 使用用户时间api

For example: in the beginning of the JS file write: performance.mark("start-script") 例如:在JS文件的开头写: performance.mark("start-script")

at the end of the JS file write: performance.mark("end-script") 在JS文件的末尾写: performance.mark("end-script")

then you can measure it too: 那么你也可以测量它:

performance.measure("total-script-execution-time", "start-script", "end-script");

This will give you the time it takes to run the entire script execution. 这将为您提供运行整个脚本执行所需的时间。

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

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