Game Development Community

Timer in script

by Imm Dtu · in Torque Game Engine · 12/16/2005 (8:47 am) · 7 replies

Hi there,

I'm trying to measure how much time some of my functions take to run from the script. For this purpose I thought I could use %beforeExecuttion = $Sim::Time; and %afterExecution = $Sim::Time;. However, this gives the same number (time). I've also tried the following:

for(%tempCount = 0; %tempCount<10000; %tempCount++)
{
echo("This should be the time: " @ $Sim::Time);
}

which also gives the same number. When I just use echo($Sim::Time); from the console, I get progressing numbers, which is exactly what I want. Why doesn't the same code work from AIPlayer.cs ? (Im sure the functions take more than 0 millisecons to run)

Hope some can help me.

#1
12/16/2005 (9:22 am)
Hi.
Try with getRealTime().
Something like %beforeExecuttion = getRealTime(); and %afterExecution = getRealTime(); should work.

Bye,
Jacopo
#2
12/16/2005 (9:51 am)
Hi again,

Thanks for the help, it seems to be working. However, in some of my functions I need more precision (the difference in time is 0), than getRealTime(); gives. (this particular function im talking about is implemented in AIPlayer.cc and called from the script (this shouldnt be a problem, right?).

Btw I've tried to use getRealTime()*1000, which still doesnt work;

Best regards
Imm
#3
12/16/2005 (10:06 am)
You could measure the time to call the function say 100 times.
- that's not really fair, as it will probably run faster 100 times in a row
than 100 times every now and then, but it might give you a measure.

- or you could implement a high-resolution timer in C and expose it to script.

(that's just the first HR timer link i found, i don't know if it's particularly good/easy)
#4
12/16/2005 (10:35 am)
Thank you both for the answers. They were very helpful :)

Best regards
/Imm
#5
12/16/2005 (11:37 am)
Your time is going to be zero, because you are checking "start" and "end" times within the same processing tick. That is never going to come up anything other than zero I would think.
#6
12/16/2005 (11:41 am)
Well, there's that, and also the fact that loops are very fast, so it could well be taking less than a millisecond to do it. :)

getRealTime() will reflect the system time, not tick time, so it's useful for timing things. But just a for loop is going to run very fast. What are you trying to do that needs such precise timing?
#7
12/16/2005 (11:41 am)
@paul - GetRealTime() calls out to the platform function GetTickCount(),
which in the case of windows "retrieves the number of milliseconds that have elapsed since the system was started."

so it's not coupled to torque's processing tick.

but still, you'll get a bunch of zeros because one millisecond is a long time these days.