On 7/9/21 5:04 PM, Ali Çehreli wrote:
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");
}
}
You can get better than hnsecs resolution with `core.time.MonoTime`,
which can support whatever the OS supports.
However, `Duration` and `SysTime` are stored in hnsecs for a very
specific reason -- range. Simply put, if you have a 64-bit integer, and
you picked nanoseconds as the unit, you can store only 585 years of
range. 10 ns gives you 5850 years, and 100 ns gives you 58k years. That
should be good enough for all but the most esoteric calculations (given
that a `Duration` is signed, this gives a range of roughly -29k years to
29k years).
Note also that hnsecs is the base unit for Windows high precision
clocks, though their epoch is year 1600 instead of year 0.
-Steve