我需要以毫秒为单位获取执行时间。
我最初问这个问题是在2008年。当时接受的答案是使用newDate().getTime()。然而,我们现在都可以同意使用标准performance.now()API更合适。因此,我将接受的答案改为这个答案。
我需要以毫秒为单位获取执行时间。
我最初问这个问题是在2008年。当时接受的答案是使用newDate().getTime()。然而,我们现在都可以同意使用标准performance.now()API更合适。因此,我将接受的答案改为这个答案。
当前回答
这是一个计时器功能。如果要测量未嵌套的多个对象之间的时间:
function timer(lap){
if(lap) console.log(`${lap} in: ${(performance.now()-timer.prev).toFixed(3)}ms`);
timer.prev = performance.now();
}
类似于console.time(),但如果不需要跟踪以前的计时器,则更容易使用。
用法:
timer() // set the start
// do something
timer('built') // logs 'built in: 591.815ms'
// do something
timer('copied') // logs 'copied in: 0.065ms'
// do something
timer('compared') // logs 'compared in: 36.41ms'
如果您喜欢console.time()中的蓝色,可以使用这行
console.log(`${lap} in: %c${(performance.now()-timer.prev).toFixed(3)}ms`, 'color:blue');
其他回答
在函数之前使用console.time(“此处的某个标签”),在函数之后使用console.timeEnd(“此处某个标签)。它将为您提供函数的运行时间。
下面是计时函数的修饰符
它包装函数,以便它们每次运行时都能计时
用法:
let test = () => { /* does something */ }
test = timed(test) // turns the function into a timed function in one line
test() // run your code as normal, logs 'function test took 1001.900ms'
这是装饰者:
let timed = (f) => (...args) => {
let start = performance.now();
let ret = f(...args);
console.log(`function ${f.name} took ${(performance.now() - start).toFixed(3)}ms`);
return ret;
}
如果您使用的是异步函数,您可以将其设置为定时异步,并在f(…args)之前添加一个await,这应该适用于这些函数。如果您希望一个装饰器同时处理同步和异步函数,则会变得更加复杂。
谢谢,Achim Koellner,我会把你的答案扩大一点:
var t0 = process.hrtime();
//Start of code to measure
//End of code
var timeInMilliseconds = process.hrtime(t0)[1]/1000000; // dividing by 1000000 gives milliseconds from nanoseconds
请注意,除了要测量的内容之外,您不应该做其他任何事情(例如,console.log也需要时间执行,这会影响性能测试)。
注意,按照异步函数执行时间的顺序,应该插入var timeInMilliseconds=process.hrtime(t0)[1]/10000;在回调中。例如
var t0 = process.hrtime();
someAsyncFunction(function(err, results) {
var timeInMilliseconds = process.hrtime(t0)[1]/1000000;
});
这是一个计时器功能。如果要测量未嵌套的多个对象之间的时间:
function timer(lap){
if(lap) console.log(`${lap} in: ${(performance.now()-timer.prev).toFixed(3)}ms`);
timer.prev = performance.now();
}
类似于console.time(),但如果不需要跟踪以前的计时器,则更容易使用。
用法:
timer() // set the start
// do something
timer('built') // logs 'built in: 591.815ms'
// do something
timer('copied') // logs 'copied in: 0.065ms'
// do something
timer('compared') // logs 'compared in: 36.41ms'
如果您喜欢console.time()中的蓝色,可以使用这行
console.log(`${lap} in: %c${(performance.now()-timer.prev).toFixed(3)}ms`, 'color:blue');
公认的答案是错误的!
由于JavaScript是异步的,因此接受的答案的变量端的值将是错误的。
var start = new Date().getTime();
for (i = 0; i < 50000; ++i) {
// JavaScript is not waiting until the for is finished !!
}
var end = new Date().getTime();
var time = end - start;
alert('Execution time: ' + time);
for的执行速度可能非常快,因此您无法看到结果是错误的。您可以使用执行某些请求的代码来测试它:
var start = new Date().getTime();
for (i = 0; i < 50000; ++i) {
$.ajax({
url: 'www.oneOfYourWebsites.com',
success: function(){
console.log("success");
}
});
}
var end = new Date().getTime();
var time = end - start;
alert('Execution time: ' + time);
因此,警报将非常迅速地提示,但在控制台中,您将看到ajax请求正在继续。
以下是您应该如何做到的:https://developer.mozilla.org/en-US/docs/Web/API/Performance.now