Merge pull request #2552 from pandamicro/optimize

Reduce performance.now invocation
This commit is contained in:
pandamicro
2018-04-17 18:39:53 +08:00
committed by GitHub
3 changed files with 29 additions and 25 deletions
+14 -10
View File
@@ -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';
+6 -6
View File
@@ -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() {
+9 -9
View File
@@ -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;