The below code creates a timer object that you can use to profile the speed of various functions in your project/game. It is intended for people who either:
a) have a slow running large/complex game system and wish to find the part causing the slow down OR
b) are obsessive micro-optimisers like me
It will output the results in a csv file that you can open with excel or openoffice to do some stats/analysis with.
(I'm afraid it uses v1 file handling as v2 file writing still does not seem to work on Mac)
Usage:1. Create a timer object - needs to be global or at least available in the whole of the scope you're going to use it in:
var time_logger = new Timer("log");
If you have multiple separate scopes and no global available you could make one in each scope with different names.
2. At the start of each portion of code you wish to time put in a record call:
time_logger.record("Update function", false);//the false parameter is optional
NOTE: the string must not contain a comma
3.The Timer will time the section until the next record call, you can end a section either by beginning another section:
time_logger.record("Render function", false);
OR by calling record with the second parameter set to true
time_logger.record("this string is ignored", true);
When true is set no new section is begun but the previous section is ended - this is so when one seciton follows another you only need one function call whereas if there's a big gap you can end the section off without creating a log entry for the gap that doesn't interest you.
4. Call the end function before you exit
time_logger.end();//this writes everything out to a file
Output is not done to a file during execution as this can significantly impact performance and hence skew the results.
function Timer(file_name)
{
this.output_file = OpenRawFile(file_name + ".csv", true);
this.output_file.write(CreateByteArrayFromString("Time,Event,TimeTaken\n"));
this.start = new Date().getTime();
this.records = [];
this.open = false;
}
Timer.prototype.record = function(status, end)
{
var time = new Date().getTime() - this.start;
var length = this.records.length;
if(this.open === true)
{
this.records[length -1].time_taken = time - this.records[length -1].time;
this.open = false;
}
if(end !== true)
{
this.records[length] = new Record(time, status);
this.open = true;
}
}
Timer.prototype.end = function()
{
var time = new Date().getTime() - this.start;
this.record("",true);
var string = "";
for(var i = 0; i < this.records.length; ++i)
{
string = string + this.records[i].time +"," +this.records[i].status +","+this.records[i].time_taken +"\n";
}
string = string +time + ", Total time running, "+ time +"\n";
this.output_file.write(CreateByteArrayFromString(string));
this.output_file.close();
}
function Record(time, status)
{
this.time = time;
this.status = status;
this.time_taken = 0;
}