Re: Real Time-ing

2015-12-09 Thread Andrea Fontana via Digitalmars-d-learn
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.





Re: Real Time-ing

2015-12-09 Thread Kagamin via Digitalmars-d-learn
On Tuesday, 8 December 2015 at 16:40:04 UTC, Taylor Hillegeist 
wrote:
However i seem to get jitter of around 1 ms. Is there anything 
else i can do to improve?


Do you want to get precision better than period of thread 
switches?


Re: Real Time-ing

2015-12-09 Thread Andrea Fontana via Digitalmars-d-learn
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?


Re: Real Time-ing

2015-12-08 Thread Kagamin via Digitalmars-d-learn

prev=now;
call();
wait(prev+dur-now);
call();


Re: Real Time-ing

2015-12-08 Thread Taylor Hillegeist via Digitalmars-d-learn

On Tuesday, 8 December 2015 at 15:50:35 UTC, Kagamin wrote:

Oops, no.

next+=dur;
wait(next-now);
call();


what calls does this use from the std library? to get the current 
time? Wait a amount of time?


Real Time-ing

2015-12-08 Thread Taylor Hillegeist via Digitalmars-d-learn
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!


Re: Real Time-ing

2015-12-08 Thread Kagamin via Digitalmars-d-learn

Oops, no.

next+=dur;
wait(next-now);
call();


Re: Real Time-ing

2015-12-08 Thread Taylor Hillegeist via Digitalmars-d-learn
On Tuesday, 8 December 2015 at 15:35:18 UTC, Taylor Hillegeist 
wrote:

So, I mostly do programming that is of run to completion


I took a stab at the problem:

http://dpaste.dzfl.pl/2eef530d00fc


0 nsecs with jitter of :5 nsecs
498937256 nsecs with jitter of :1062744 nsecs
499036173 nsecs with jitter of :963827 nsecs
500025650 nsecs with jitter of :25650 nsecs
500020735 nsecs with jitter of :20735 nsecs
499057062 nsecs with jitter of :942938 nsecs
498932033 nsecs with jitter of :1067967 nsecs
591037317 nsecs with jitter of :91037317 nsecs
499032794 nsecs with jitter of :967206 nsecs
499034637 nsecs with jitter of :965363 nsecs
499022963 nsecs with jitter of :977037 nsecs
498976577 nsecs with jitter of :1023423 nsecs
499076723 nsecs with jitter of :923277 nsecs
499023885 nsecs with jitter of :976115 nsecs
499018048 nsecs with jitter of :981952 nsecs
499004224 nsecs with jitter of :995776 nsecs
499048461 nsecs with jitter of :951539 nsecs
499013747 nsecs with jitter of :986253 nsecs
499018048 nsecs with jitter of :981952 nsecs
499007604 nsecs with jitter of :992396 nsecs

However i seem to get jitter of around 1 ms. Is there anything 
else i can do to improve?


Re: Real Time-ing

2015-12-08 Thread Chris Wright via Digitalmars-d-learn
On Tue, 08 Dec 2015 15:35:18 +, 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?

Busy-wait and access the CPU's high precision clock.

If you have an extended wait and don't want to busy-wait the whole time, 
sleep up to, say, 5ms before the time you want to call the function, then 
busy-wait.


Re: Real Time-ing

2015-12-08 Thread BBaz via Digitalmars-d-learn
On Tuesday, 8 December 2015 at 16:40:04 UTC, Taylor Hillegeist 
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


I took a stab at the problem:

http://dpaste.dzfl.pl/2eef530d00fc


0 nsecs with jitter of :5 nsecs
498937256 nsecs with jitter of :1062744 nsecs
499036173 nsecs with jitter of :963827 nsecs
500025650 nsecs with jitter of :25650 nsecs
500020735 nsecs with jitter of :20735 nsecs
499057062 nsecs with jitter of :942938 nsecs
498932033 nsecs with jitter of :1067967 nsecs
591037317 nsecs with jitter of :91037317 nsecs
499032794 nsecs with jitter of :967206 nsecs
499034637 nsecs with jitter of :965363 nsecs
499022963 nsecs with jitter of :977037 nsecs
498976577 nsecs with jitter of :1023423 nsecs
499076723 nsecs with jitter of :923277 nsecs
499023885 nsecs with jitter of :976115 nsecs
499018048 nsecs with jitter of :981952 nsecs
499004224 nsecs with jitter of :995776 nsecs
499048461 nsecs with jitter of :951539 nsecs
499013747 nsecs with jitter of :986253 nsecs
499018048 nsecs with jitter of :981952 nsecs
499007604 nsecs with jitter of :992396 nsecs

However i seem to get jitter of around 1 ms. Is there anything 
else i can do to improve?


1) a computer is not real-time, for example audio is always 
buffered (e.g 512 samples)


2) thread are not good for timing. In a simple test you could get 
satisfying results while in a stressed IRL environment it won't 
work as well. so you should rather use the OS API to make a timer 
(or the one proposed by a framework if it applies, for example 
gui app with SDL: SDL timer).


However if you want to have better times you can use core.time in 
a thread callback:


~
// to, t1 and interval are uint;
if (!t0) t0 = TickDuration.currSystemTick.msecs;
t1 = TickDuration.currSystemTick.msecs;

if ((t1 - t0) > interval)
{
t0 = 0;
onTimer();
}
~