High precision timer in Pascal

As a challenge to myself I’ve been looking at writing my Competition bots in pascal. But I’m doing things like monte-carlo and genetic algorithms that need to be stopped within the time-limit allowed per turn. In C++ I’ve been using the high_precision_clock. It’s equivalent in Pascal would probably be EpikTimer. However, it’s not part of a standard library and thus has to be copies from source into my programs. That’s quite a lot of code and I’ve read reports that in some cases it slows-down code up to factor 10!

I’ve also found QueryPerformanceCounter and GetMsCount, but both seem to be designed for Windows applications.

Somebody suggested

Function GetTimeInMilliSeconds(theTime : TTime): Int64;
var Hour, Min, Sec, MSec: Word;
begin
    DecodeTime(theTime,Hour, Min, Sec, MSec);
    Result := (Hour * 3600000) + (Min * 60000) + (Sec * 1000) + MSec;
end;

IntToStr(GetTimeInMilliSeconds(Time));

But to my understanding this is very inaccurate (25 ms error?)

Does anybody have and idea how to get ms-accurate timing in pascal?

1 Like

If EpikTimer is accurate, and even if it is slow, did you try to measure time left often?
For example do hundreds of simulations between two time calls.
Even if you have to decrease your timeout, this is usually faster, even in C++.

I don’t check timeout at every iteration, it’s too slow, even in C++. So most of the time i have something like this :

while (!timeout()) {
    for (int i = 0; i < 100; ++i) {
        doSomething();
    }
}

Thanks, but my question is specifically of how to check the time accurately in pascal… I know how to do it in C++, including not checking every iteration…

I think I’ve solved it: GetTickCount64() is directly available and gives ms-accuracy. It seems to work good enough!
Thanks for thinking along.