This is the code I made for Baseplate:
function Baseplate::__benchmark__(%this,%callback) {
cancel(%this.benchmarkSchedules[%callback]);
if(%this.benchmarks[%callback] <= 0)
return call(%callback,getSimTime() - %this.benchmarkStart[%callback]);
%this.benchmarks[%callback]--;
%this.benchmarkSchedules[%callback] = %this.schedule(0,"__benchmark__",%callback);
call(%callback);
}
function Baseplate::benchmark(%this,%callback,%times) {
if(isEventPending(%this.benchmarkSchedules[%callback]) || %times < 1 || !isFunction(%callback))
return %this;
%this.benchmarkStart[%callback] = getSimTime();
%this.benchmarks[%callback] = %times;
%this.benchmarkSchedules[%callback] = %this.schedule(0,"__benchmark__",%callback);
}
You use it like so:
function test1(%time) {
if(%time) // completed
echo(%time / 300);
else {
// do something to benchmark
}
}
_.benchmark(test1, 300); // benchmark the test1 function 300 times
You'll notice it's tested 300 times, this is to keep a consistent amount. To find the average of calling the function once, you would divide it by the number of tests.
Firstly, TorqueScript is going to work one of these ways, but I'm not sure which:
- Event loop. Each task is given a priority. Say I start a schedule for 20ms but I also have the main task, it would do both of them at once by balancing them by priority.
- Threaded. Each task is executed in a completely different thread. Starting a schedule would put it in a completely different thread in order to make sure the main task isn't blocked nor does it block the scheduled task.
Currently you're running it in the main task, which for either of the above means it's going to be low priority. Putting it in it's own task will ensure the highest accuracy. As to why you're getting negative numbers.. I have no clue, it doesn't happen to me when I mirror your code.