Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
November 17, 2014 Simple timing | ||||
---|---|---|---|---|
| ||||
I'm trying to write a program that involves simple timing; I like to be able to execute some function at a point no sooner than, say, 3500 milliseconds from now so I need to read the current 'system time' in ticks and calculate the required point in the future using ticks per sec. In other languages I've done something like this (pseudo code). now = currentTime; target = now + 3500 do something .. until currentTime > target execute function I'm completely new to D and find the help pages on this subject very confusing (or at least a bit too detailed for a beginner!). Can anyone point me to a simple example or tell me how to go about this? Many thanks Paul |
November 17, 2014 Re: Simple timing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul | The easiest way isn't to read the clock at all and instead just put your program to sleep for a while: void main() { import std.stdio; import core.thread; writeln("started"); Thread.sleep(3500.msecs); writeln("ended"); } |
November 17, 2014 Re: Simple timing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul | On Monday, 17 November 2014 at 16:24:10 UTC, Paul wrote:
> I'm trying to write a program that involves simple timing; I like to be able to execute some function at a point no sooner than, say, 3500 milliseconds from now so I need to read the current 'system time' in ticks and calculate the required point in the future using ticks per sec. In other languages I've done something like this (pseudo code).
>
> now = currentTime;
> target = now + 3500
>
> do something
> ..
> until currentTime > target
> execute function
>
>
> I'm completely new to D and find the help pages on this subject very confusing (or at least a bit too detailed for a beginner!). Can anyone point me to a simple example or tell me how to go about this?
>
> Many thanks
>
> Paul
You can get the current 'system time' from std.datetime using Clock.currTime.
Subtracting one SysTime from another results in a 'Duration', which you can then compare to your target duration:
var startTime = Clock.currTime;
doSomething();
while(Clock.currTime - startTime < 3500.msecs)
{
executeFunction();
}
Clock.currTime uses a high performance timer, QueryPerformanceCounter on Windows for example, so you shouldn't have to worry about timer accuracy.
|
November 17, 2014 Re: Simple timing [solved] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rene Zwanenburg | Thank you both, I'm sure that answers my question.
Paul
On Monday, 17 November 2014 at 16:38:45 UTC, Rene Zwanenburg wrote:
> On Monday, 17 November 2014 at 16:24:10 UTC, Paul wrote:
>> I'm trying to write a program that involves simple timing; I like to be able to execute some function at a point no sooner than, say, 3500 milliseconds from now so I need to read the current 'system time' in ticks and calculate the required point in the future using ticks per sec. In other languages I've done something like this (pseudo code).
>>
>> now = currentTime;
>> target = now + 3500
>>
>> do something
>> ..
>> until currentTime > target
>> execute function
>>
>>
>> I'm completely new to D and find the help pages on this subject very confusing (or at least a bit too detailed for a beginner!). Can anyone point me to a simple example or tell me how to go about this?
>>
>> Many thanks
>>
>> Paul
>
> You can get the current 'system time' from std.datetime using Clock.currTime.
>
> Subtracting one SysTime from another results in a 'Duration', which you can then compare to your target duration:
>
> var startTime = Clock.currTime;
>
> doSomething();
>
> while(Clock.currTime - startTime < 3500.msecs)
> {
> executeFunction();
> }
>
> Clock.currTime uses a high performance timer, QueryPerformanceCounter on Windows for example, so you shouldn't have to worry about timer accuracy.
|
November 19, 2014 Re: Simple timing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rene Zwanenburg | On Monday, 17 November 2014 at 16:38:45 UTC, Rene Zwanenburg wrote:
> Clock.currTime uses a high performance timer, QueryPerformanceCounter on Windows for example, so you shouldn't have to worry about timer accuracy.
You probably mistake it for StopWatch, clock is not timer.
|
Copyright © 1999-2021 by the D Language Foundation