Better than "Clock.currStdTime()/10000000"
Seb via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Feb 15 06:02:19 PST 2017
On Wednesday, 15 February 2017 at 13:19:57 UTC, berni wrote:
> I need to measure time elapsed in seconds, like this:
>
>> auto start = Clock.currStdTime();
>> // some stuff
>> auto stop = Clock.currStdTime();
>> auto duration = (stop-start)/10000000;
>
> This works, but I wonder if there is something better that
> using the magic constant 10000000. I read about 10.secs giving
> the duration of 10 seconds, but I don't understand how to adapt
> this to my case. I've read the documentation of core.time,
> std.datetime (and the Introduction to this package) but I can't
> make head or tail of it.
>
> PS: It's not about benchmarking. I'd like to show the user the
> time elapsed.
How about something like this:
import std.stdio;
void main()
{
import core.thread, core.time;
import std.conv, std.datetime;
auto start = Clock.currTime;
Thread.sleep(500.msecs);
auto diff = Clock.currTime - start;
diff.writeln; // Duration
(diff.total!"msecs").writeln;
(to!("msecs", long) (diff.to!TickDuration)).writeln;
(to!("msecs", double)(diff. to!TickDuration)).writeln;
}
Note that TickDuration is deprecated.
More information about the Digitalmars-d-learn
mailing list