Delta Time - Streamlined Time Keeping For Games

cc cc at nevernet.com
Sat Sep 3 20:52:02 UTC 2022


On Sunday, 21 August 2022 at 22:54:57 UTC, jordan4ibanez wrote:
> Ohh I get it. No I'm not doing anything that advance with 
> atomic clocks. I cannot change forum posts so let's just leave 
> it at: "I tried to make this as accurate as possible for your 
> machine down to the nanosecond" :P

MonoTime is not necessarily returning a value with nanosecond 
accuracy, however.  On Windows, QueryPerformanceCounter is used 
(by default, other implementations may be available), which in 
this case returns values in Ticks of hecto-nanoseconds, so calls 
to `duration.total!("nsecs")` are going to return a multiple of 
100.

MonoTime reports its internal values through `.ticks` and 
`.ticksPerSecond`, so I like to use those.  Ultimately the end 
result comes out to the same, but it avoids additional 
conversions and a magic number in the code, and maybe provides a 
little future proofing against potential exotic architectures.

```d
writefln("%,d", MonoTime.ticksPerSecond); // 10,000,000
double doubleTicksPerSecond = cast(double) 
MonoTime.ticksPerSecond;
long lastTicks = MonoTime.currTime.ticks;
...
// loop
	long nowTicks = MonoTime.currTime.ticks;
	delta = cast(double)(nowTicks - lastTicks) / 
doubleTicksPerSecond;
	lastTicks = nowTicks;
```

https://docs.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancefrequency
https://docs.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancecounter



More information about the Digitalmars-d mailing list