SRL Archive - Research & Development Lounge

Real timing of functions!


nielsie95

2008-08-11 14:03:40

Hey,

Ever wanted to really time your functions in scar? With GetTickCount you have a 16ms margin. This - make sure API calls are enabled - is way more precise:


program New;

function QueryPerformanceFrequency(out Frequency: Int64): LongBool; external 'QueryPerformanceFrequency@kernel32.dll stdcall';
function QueryPerformanceCounter(out Counter: Int64): LongBool; external 'QueryPerformanceCounter@kernel32.dll stdcall';

procedure MarkTime(var Time: Int64);
var
Freq: Int64;
begin
if QueryPerformanceFrequency(Freq) then
QueryPerformanceCounter(Time)
else
Time := GetTickCount;
end;

function TimeFromMark(Mark: Int64): Double;
var
Freq, Now: Int64;
begin
if QueryPerformanceFrequency(Freq) then
begin
QueryPerformanceCounter(Now);
Result := ((Now - Mark) / Freq) * 1000;
end
else
Result := (GetTickCount - Mark);
end;

var
Time: Int64;
begin
MarkTime(Time);
WriteLn('Straight from MarkTime took ' + FloatToStr(TimeFromMark(Time)) + ' ms.');
Wait(10);
WriteLn('After another Wait(10) we are ' + FloatToStr(TimeFromMark(Time)) + ' ms away from the mark.');
end.

Cheers,
Nielsie95

Gu3st...

2008-08-11 14:08:11

I dont get what it does, but when i run it i get an error:

Failed when compiling
Line 4: [Error] (4:1): Semicolon (';') expected in script


EDIT: I turned API Calls on and now it works.

EDIT2: Ah now i get it, an extreme precise timing function!
Successfully compiled (55 ms)
Straight from MarkTime took 0,0310793690259516 ms.
After another Wait(10) we are 10,8135950239486 ms away from the mark.
Successfully executed

mixster

2008-08-16 06:21:05

I'm sure I left a comment XD, anyway.
Nice nielsie, I loves it a lot - so much easier that running the same command 1000 times and you get much better accuracy, though surely you should get the average frequency to make it more accurate as I'm sure it alters slightly during usage :)

boberman

2008-08-16 06:56:37

Good, but I don't know if the extra overhead is really all that worth it. After all, most add a random onto their timer anyways :D. 16ms is peanuts when you usually deal with numbers greater then 500.

However, if you have some some exact action you want to perform then I guess this could help.

Timer

2008-08-16 09:40:00

Straight from MarkTime took 0.0144396390090248 ms.
After another Wait(10) we are 10.2136146596335 ms away from the mark.

Is that good? :redface: Btw, I have other programs running! :p

nielsie95

2008-08-20 13:38:31

I know 16ms isn't much, but it looks way more professional if you can show how fast your function is with a bunch of numbers behind the comma. :p

bullzeye95

2008-08-20 14:22:32

Interesting. I've always assumed that the 16 MS margin was unavoidable while using windows.
Thanks.

Infantry001

2009-08-17 22:54:29

I don't think the bump would really matter here…

Thanks, nielsie! That's pretty cool =)