SRL Archive - Research & Development Lounge

High-precision timer for Simba[plugin]


CynicRus

2012-12-27 02:19:03

Hi. This morning at work I had nothing to do, and I wrote this plugin. It is the realization of high-precision timer for Simba. For those who require special accuracy.


program new;
{$loadlib hpTimer}
var
A, B : LongWord;
T: int64;
begin
StartTimer(T);
A := GetTick;
WaitMicroseconds(30000); // 30ms
B := GetTick;
StopTimer(T);
WriteLn(toStr('Milliseconds ellapsed:' + MillisecondsElapsed(T, false)));
WriteLn(toStr(TickDelta(A, B)));
WriteLn(toStr(TickDeltaW(A, B)));
end.
Output:

Compiled successfully in 16 ms.
24
31
31
Successfully executed.

Description:
Export list:

procedure StartTimer(var Timer: Int64);

procedure StopTimer(var Timer: Int64);

procedure ResumeTimer(var StoppedTimer: Int64);

procedure InitStoppedTimer(var Timer: Int64);

procedure InitElapsedTimer(var Timer: Int64; const Milliseconds: Integer);

function MillisecondsElapsed(const Timer: Int64;const TimerRunning: Boolean): Integer;

procedure WaitMicroseconds(const MicroSeconds: Integer);

- StartTimer returns an encoded time (running timer).

- StopTimer returns an encoded elapsed time (stopped timer).

- ResumeTimer returns an encoded time (running timer), given an encoded elapsed time (stopped timer).
- InitStoppedTimer returns an encoded elapsed time of zero, ie a stopped timer with no time elapsed.
- MillisecondsElapsed returns elapsed time for a timer in milliseconds.

- WaitMicroSeconds goes into a tight loop for the specified duration. It should be used where short and accurate delays are required.

Written in Lazarus, cross-platform, a sources available in attachment:)

Merry Christmas!
Cheers, Cynic.

nielsie95

2012-12-27 12:02:46

Pretty useful indeed! Perhaps a list of exported functions and what they do? ;)
Here's the non-plugin version for Windows.

CynicRus

2012-12-27 12:35:13

Documentation will be later-)

CynicRus

2012-12-28 01:13:21

Description:
Export list:

procedure StartTimer(var Timer: Int64);

procedure StopTimer(var Timer: Int64);

procedure ResumeTimer(var StoppedTimer: Int64);

procedure InitStoppedTimer(var Timer: Int64);

procedure InitElapsedTimer(var Timer: Int64; const Milliseconds: Integer);

function MillisecondsElapsed(const Timer: Int64;const TimerRunning: Boolean): Integer;

procedure WaitMicroseconds(const MicroSeconds: Integer);

- StartTimer returns an encoded time (running timer).

- StopTimer returns an encoded elapsed time (stopped timer).

- ResumeTimer returns an encoded time (running timer), given an encoded elapsed time (stopped timer).
- InitStoppedTimer returns an encoded elapsed time of zero, ie a stopped timer with no time elapsed.
- MillisecondsElapsed returns elapsed time for a timer in milliseconds.

- WaitMicroSeconds goes into a tight loop for the specified duration. It should be used where short and accurate delays are required.