我需要以毫秒为单位获取执行时间。
我最初问这个问题是在2008年。当时接受的答案是使用newDate().getTime()。然而,我们现在都可以同意使用标准performance.now()API更合适。因此,我将接受的答案改为这个答案。
我需要以毫秒为单位获取执行时间。
我最初问这个问题是在2008年。当时接受的答案是使用newDate().getTime()。然而,我们现在都可以同意使用标准performance.now()API更合适。因此,我将接受的答案改为这个答案。
当前回答
使用Firebug,同时启用Console和Javascript。单击配置文件。重新加载再次单击配置文件。查看报告。
其他回答
这是一个计时器功能。如果要测量未嵌套的多个对象之间的时间:
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');
使用Firebug,同时启用Console和Javascript。单击配置文件。重新加载再次单击配置文件。查看报告。
具有累积循环的秒表
与服务器和客户端(节点或DOM)一起工作,使用Performance API。当您有许多小循环时(例如,在处理1000个数据对象的调用1000次的函数中)很好,但您希望了解该函数中的每个操作如何相加。
所以这一个使用了模块全局(singleton)计时器。与类单例模式相同,只是使用起来稍微简单一点,但您需要将其放在一个单独的例如秒表.js文件中。
const perf = typeof performance !== "undefined" ? performance : require('perf_hooks').performance;
const DIGITS = 2;
let _timers = {};
const _log = (label, delta?) => {
if (_timers[label]) {
console.log(`${label}: ` + (delta ? `${delta.toFixed(DIGITS)} ms last, ` : '') +
`${_timers[label].total.toFixed(DIGITS)} ms total, ${_timers[label].cycles} cycles`);
}
};
export const Stopwatch = {
start(label) {
const now = perf.now();
if (_timers[label]) {
if (!_timers[label].started) {
_timers[label].started = now;
}
} else {
_timers[label] = {
started: now,
total: 0,
cycles: 0
};
}
},
/** Returns total elapsed milliseconds, or null if stopwatch doesn't exist. */
stop(label, log = false) {
const now = perf.now();
if (_timers[label]) {
let delta;
if(_timers[label].started) {
delta = now - _timers[label].started;
_timers[label].started = null;
_timers[label].total += delta;
_timers[label].cycles++;
}
log && _log(label, delta);
return _timers[label].total;
} else {
return null;
}
},
/** Logs total time */
log: _log,
delete(label) {
delete _timers[label];
}
};
export default class Singleton {
static myInstance: Singleton = null;
_timers: any = {};
/**
* @returns {Singleton}
*/
static getInstance() {
if (Singleton.myInstance == null) {
Singleton.myInstance = new Singleton();
}
return this.myInstance;
}
initTime(label: string) {
this._timers[label] = Date.now();
return this._timers[label];
}
endTime(label: string) {
const endTime = Date.now();
if (this._timers[label]) {
const delta = endTime - this._timers[label];
const finalTime = `${label}: ${delta}ms`;
delete this._timers[label];
return finalTime;
} else {
return null;
}
}
}
与字符串相关的InitTime。
return Singleton.getInstance().initTime(标签);//返回时间init
return Singleton.getInstance().endTime(标签);//返回初始化和结束之间的总时间
几个月前,我用Date.now()编写了自己的例程,对函数进行计时——尽管当时接受的方法似乎是performance.now(--因为性能对象在稳定的Node.js版本中还不可用(内置)。
今天我做了更多的研究,发现了另一种计时方法。由于我还发现了如何在Node.js代码中使用它,所以我想在这里分享一下。
以下内容结合了w3c和Node.js给出的示例:
function functionTimer() {
performance.mark('start')
functionToBeTimed()
performance.mark('end')
performance.measure('Start to End', 'start', 'end')
const measure = performance.getEntriesByName('Start to End')[0]
console.log(measure.duration)
}
注:
如果您打算在Node.js应用程序中使用性能对象,则必须包含以下要求:const{performance}=require('perf_hooks')