Can I get the time "Duration" in "nsecs" acurracy?

Ali Çehreli acehreli at yahoo.com
Fri Jul 9 21:04:42 UTC 2021


On 7/9/21 1:54 PM, Paul Backus wrote:
> On Friday, 9 July 2021 at 20:43:48 UTC, rempas wrote:
>> I'm reading the library reference for 
>> [core.time](https://dlang.org/phobos/core_time.html#Duration) and It 
>> says that the duration is taken in "hnsecs" and I cannot understand if 
>> we can change that and choose the precision. Does anyone know if we 
>> can do that?
> 
> It is stored internally in "hnsecs", but you can convert it to other 
> units using the `total` method [1]; for example, 
> `myDuration.total!"nsecs"`.
> 
> [1] https://dlang.org/phobos/core_time.html#.Duration.total

Yes but the resolution seems not to be better than 100 nsecs. A quick 
research reveals a better resolution is not possible with common 
hardware on at least Linux.

The following program always prints multiples of 100 on my Mint OS:

import std.stdio;
import core.thread;
import std.datetime.stopwatch;

void main() {
   auto sw = StopWatch();
   sw.start();

   foreach (_; 0..10) {
     Thread.sleep(0.nsecs);
     writefln("%,s", sw.peek.total!"nsecs");
   }
}

Ali


More information about the Digitalmars-d-learn mailing list