Real Time-ing

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


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?


More information about the Digitalmars-d-learn mailing list