Skip to content

Instantly share code, notes, and snippets.

@wivlaro
Created May 5, 2013 16:16
Show Gist options
  • Save wivlaro/5521284 to your computer and use it in GitHub Desktop.
Save wivlaro/5521284 to your computer and use it in GitHub Desktop.
Simple averaging profiler
function ProfilingTimer() {
this.currentStart = null;
this.times = [];
this.index = 0;
}
ProfilingTimer.prototype.start = function() {
this.currentStart = new Date().getTime();
};
ProfilingTimer.prototype.stop = function() {
this.times[this.index] = new Date().getTime() - this.currentStart;
this.index = (this.index + 1) % 60;
};
ProfilingTimer.prototype.infoString = function() {
var min = this.times[0], max = min, ave = min;
for (var i = 1 ; i < this.times.length; i++) {
var t = this.times[i];
if (t < min) min = t; else if (t > max) max = t;
ave += t;
}
ave /= this.times.length;
return 'ave=' + ave.toFixed(1) + ', range=' + min + '-' + max;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment