如何测量脚本从开始运行到结束所需的时间?
start-timing
//CODE
end-timing
EDIT:2011 年 1 月,这是最佳的可用解决方案。其他解决方案(例如performance.now()
现在应该是首选。
var start = new Date();
// CODE
var time = new Date() - start;
// time is the number of milliseconds it taken to execute the script
您可能还想将其包装在一个函数中:
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);
如果您尝试分析您的代码,您可能需要尝试Firebug http://getfirebug.com/扩展,其中包括一个 javascript 分析器。它有一个很棒的用户界面来进行分析,但也可以通过它的编程方式完成控制台API http://getfirebug.com/wiki/index.php/Console_API#console.time.28name.29:
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.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)