如何测量函数执行所花费的时间 [英] How to measure time taken by a function to execute
本文介绍了如何测量函数执行所花费的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要以毫秒为单位获得执行时间。
I need to get execution time in milliseconds.
我最初在2008年问过这个问题。接受的答案
然后是使用 new Date()。getTime( )但是,我们现在都同意
使用标准 performance.now() API更适合
。因此,我正在改变接受此答案的答案。
I originally asked this question back in 2008. The accepted answer then was to use new Date().getTime() However, we can all agree now that using the standard performance.now() API is more appropriate. I am therefore changing the accepted answer to this one.
推荐答案
使用 performance.now() :
Using performance.now():
var t0 = performance.now();
doSomething(); // <---- The function you're measuring time for
var t1 = performance.now();
console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.")
NodeJs
:导入性能
类
使用 console.time : (非标准) (生活标准)
Using console.time: (non-standard) (living standard)
console.time('someFunction');
someFunction(); // Whatever is timed goes between the two "console.time"
console.timeEnd('someFunction');
注意:
字符串传递到 time()
和 timeEnd()
方法必须匹配
(对于计时器)按预期完成。
console.time()
文件:
console.time()
documentations:
- 关于的NodeJS文档
- MDN(客户端)文档
- NodeJS documentation regarding
- MDN (client-side) documentation
这篇关于如何测量函数执行所花费的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文