Javascript 如何度量脚本的执行时间?

Javascript 如何度量脚本的执行时间?,javascript,timer,Javascript,Timer,如何衡量脚本从开始运行到结束所需的时间 start-timing //CODE end-timing 编辑:2011年1月,这是最好的解决方案。现在应该首选其他解决方案(如performance.now()) 您可能还希望将其包装到函数中: function time_my_script(script) { var start = new Date(); script(); return new Date() - start; } // call it li

如何衡量脚本从开始运行到结束所需的时间

 start-timing
   //CODE
 end-timing
编辑:2011年1月,这是最好的解决方案。现在应该首选其他解决方案(如
performance.now()

您可能还希望将其包装到函数中:

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);
如果您试图评测代码,您可能希望尝试扩展,其中包括javascript评测器。它有一个很棒的评测用户界面,但也可以通过其以下功能以编程方式完成:


使用
performance.now()
而不是
new Date()
。这将提供更准确、更好的结果。请参见此答案这里有一个类似秒表的快速功能

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
使用 例如: 在JS文件的开头写:
performance.mark(“开始脚本”)

在JS文件的末尾写:
performance.mark(“结束脚本”)

然后你也可以测量它:


性能度量(“总脚本执行时间”、“开始脚本”、“结束脚本”);


这将为您提供运行整个脚本执行所需的时间。

记住,语法正确的标题和相关标记会产生很大的差异。这是否回答了您的问题?您不需要调用
getTime()
:使用
-
操作符将每个
日期
对象转换为其时间值。例如,
返回新日期()-start;
让我惊讶的是,有时解决方案是如此的简单和优雅!:-)
性能。现在()
应该比
新日期()更受欢迎
-请参阅Morteza Ziaeemehr的其他答案。@PierreArnaud请在投票前检查日期(:@arnaud576875您是对的-我在您的答案的开头添加了一条注释,以清楚地表明答案可追溯到2011年1月。今天还存在其他解决方案。
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.
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