From 01e6fcf943209cabb4c2398b51ebe6c064fd73f7 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Tue, 17 Apr 2018 18:31:19 +0800 Subject: [PATCH] Reduce performance.now invocation --- cocos2d/core/utils/profiler/CCProfiler.js | 24 ++++++++++++--------- cocos2d/core/utils/profiler/counter.js | 12 +++++------ cocos2d/core/utils/profiler/perf-counter.js | 18 ++++++++-------- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/cocos2d/core/utils/profiler/CCProfiler.js b/cocos2d/core/utils/profiler/CCProfiler.js index 88487ea843..df367e65bb 100644 --- a/cocos2d/core/utils/profiler/CCProfiler.js +++ b/cocos2d/core/utils/profiler/CCProfiler.js @@ -121,8 +121,9 @@ function generateStats () { mode: { desc: cc.game.renderType === cc.game.RENDER_TYPE_WEBGL ? 'WebGL' : 'Canvas', min: 1 } }; + let now = performance.now(); for (let id in _stats) { - _stats[id]._counter = new PerfCounter(id, _stats[id]); + _stats[id]._counter = new PerfCounter(id, _stats[id], now); } } @@ -163,18 +164,20 @@ function generateNode () { function beforeUpdate () { generateNode(); - _stats['frame']._counter.start(); - _stats['logic']._counter.start(); + let now = cc.director._lastUpdate; + _stats['frame']._counter.start(now); + _stats['logic']._counter.start(now); } function afterUpdate () { + let now = performance.now(); if (cc.director.isPaused()) { - _stats['frame']._counter.start(); + _stats['frame']._counter.start(now); } else { - _stats['logic']._counter.end(); + _stats['logic']._counter.end(now); } - _stats['render']._counter.start(); + _stats['render']._counter.start(now); } function updateLabel (stat) { @@ -185,16 +188,17 @@ function updateLabel (stat) { } function afterDraw () { - _stats['render']._counter.end(); + let now = performance.now(); + _stats['render']._counter.end(now); _stats['draws']._counter.value = cc.renderer.drawCalls; - _stats['frame']._counter.end(); - _stats['fps']._counter.frame(); + _stats['frame']._counter.end(now); + _stats['fps']._counter.frame(now); let left = ''; let right = ''; for (let id in _stats) { let stat = _stats[id]; - stat._counter.sample(); + stat._counter.sample(now); left += stat.desc + '\n'; right += stat._counter.human() + '\n'; diff --git a/cocos2d/core/utils/profiler/counter.js b/cocos2d/core/utils/profiler/counter.js index 681e91c900..551619947f 100644 --- a/cocos2d/core/utils/profiler/counter.js +++ b/cocos2d/core/utils/profiler/counter.js @@ -1,5 +1,5 @@ class Counter { - constructor(id, opts) { + constructor(id, opts, now) { this._id = id; this._opts = opts || {}; @@ -8,15 +8,15 @@ class Counter { this._averageValue = 0; this._accumValue = 0; this._accumSamples = 0; - this._accumStart = window.performance.now(); + this._accumStart = now; } - _average(v) { + _average(v, now) { if (this._opts.average) { this._accumValue += v; ++this._accumSamples; - let t = performance.now(); + let t = now; if (t - this._accumStart >= this._opts.average) { this._averageValue = this._accumValue / this._accumSamples; this._accumValue = 0; @@ -31,8 +31,8 @@ class Counter { this._value = v; } - sample() { - this._average(this._value); + sample(now) { + this._average(this._value, now); } human() { diff --git a/cocos2d/core/utils/profiler/perf-counter.js b/cocos2d/core/utils/profiler/perf-counter.js index 8f8864b0aa..778810ec0a 100644 --- a/cocos2d/core/utils/profiler/perf-counter.js +++ b/cocos2d/core/utils/profiler/perf-counter.js @@ -1,25 +1,25 @@ const Counter = require('./counter'); class PerfCounter extends Counter { - constructor(id, opts) { - super(id, opts); + constructor(id, opts, now) { + super(id, opts, now); // DISABLE // this._idstart = `${id}_start`; // this._idend = `${id}_end`; - this._time = window.performance.now(); + this._time = now; } - start() { - this._time = window.performance.now(); + start(now) { + this._time = now; // DISABLE: long time running will cause performance drop down // window.performance.mark(this._idstart); } - end() { - this._value = window.performance.now() - this._time; + end(now) { + this._value = now - this._time; // DISABLE: long time running will cause performance drop down // window.performance.mark(this._idend); @@ -33,8 +33,8 @@ class PerfCounter extends Counter { this.start(); } - frame() { - let t = window.performance.now(); + frame(now) { + let t = now; let e = t - this._time; this._total++; let avg = this._opts.average || 1000;