Skip to content

Instantly share code, notes, and snippets.

@gcoop
Created May 4, 2011 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gcoop/954962 to your computer and use it in GitHub Desktop.
Save gcoop/954962 to your computer and use it in GitHub Desktop.
Benchmark.js
/**
* Simple benchmark class in Titanium.
*
* @usage
* Benchmark.start();
* Benchmark.addMarker("Did some cool stuff.");
* Benchmark.end(); // Print that shit out.
*
* @author Gavin Cooper.
*/
Benchmark = (function () {
var markers = [];
return {
start: function () {
markers = [];
markers.push({title: "Start", value: new Date()});
},
addMarker: function (title) {
if (markers.length <= 0) {
Benchmark.start();
}
markers.push({title: title, value: new Date()});
},
end: function () {
markers.push({title: "End", value: new Date()});
Ti.API.debug("==== BENCHMARK START ====");
for (var i = 1; i < markers.length; i++) {
var duration = markers[i].value.getTime() - markers[i - 1].value.getTime();
Ti.API.debug("==== "+markers[i].title+" - "+duration+" m/s");
if (i == (markers.length - 1)) {
Ti.API.debug("==== Total Time: "+(markers[i].value.getTime() - markers[0].value.getTime())+" m/s");
}
}
Ti.API.debug("==== BENCHMARK END ====");
markers = [];
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment