Real Time-ing

Andrea Fontana via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Dec 9 02:06:34 PST 2015


On Wednesday, 9 December 2015 at 10:00:46 UTC, Andrea Fontana 
wrote:
> On Tuesday, 8 December 2015 at 15:35:18 UTC, Taylor Hillegeist 
> wrote:
>> So, I mostly do programming that is of run to completion 
>> verity. But I have a dream of calling functions periodically. 
>> So my question is:
>>
>> What is the best (most time accurate) way to call a function 
>> every n time units?
>> What is the best way to measure the jitter of these calls?
>>
>>
>> I'm also interested in waiting vs calling periodically eg.
>>
>> call
>> wait(1 ms)
>> call
>>
>> is not the same as 1 ms from call to call, due to the time 
>> duration of the function call.
>>
>> Thanks!
>
> import core.thread;
> import std.datetime;
> import std.algorithm.comparison;
> import std.math;
> import std.stdio;
> import core.time;
>
> long loopTime = 0;
> long CmdTime = 500_000_000; //Time in ns
>
> void main()
> {
>         for(int x = 0; x<20; x++){
>                 auto time = TickDuration.currSystemTick.nsecs;
>                 myPrinter(loopTime);
>                 while(TickDuration.currSystemTick.nsecs - time 
> < CmdTime){}
>                 loopTime = TickDuration.currSystemTick.nsecs - 
> time;
>         }
> }
>
> void myPrinter(long time){
>
>         writeln(time," nsecs with jitter of :", 
> abs(CmdTime-time), " nsecs");
> }
>
> What about this?

Anyway in order to avoid error accumulation it's probably a good 
idea not to reset timer every time if possibile.

If you know the operations you need to perform, probably it works 
better to store target time rather than delta.

So instead of:
- move x for 5 seconds
- move y for 3 seconds
- move z for 4 seconds
- ...

I think this works better:
- move x since second 5
- move y since second 8
- move z since second 12

So errors won't accumulate.





More information about the Digitalmars-d-learn mailing list