Measuring Execution time
Jonathan M Davis via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Jul 23 17:58:01 PDT 2015
On Thursday, July 23, 2015 13:59:11 Steven Schveighoffer via Digitalmars-d-learn wrote:
> On 7/22/15 5:23 AM, Clayton wrote:
> > How does one represent Duration in only Micro-seconds, or milliseconds.
> > Trying to measure the execution time of an algorithm and I get "4 ms,
> > 619 μs, and 8 hnsecs" , I want to sum all these and get total hnsecs or
> > μs .
> >
> > I would also appreciate advise on whether this is the best way to
> > measure the execution time of an algorithm.
> >
> >
> >
> > import std.datetime;
> > import std.stdio;
> >
> > void algorithm( ){
> > writeln("Hello!");
> > }
> > void main(){
> >
> > auto stattime = Clock.currTime();
> > algorithm( );
> > endttime = Clock.currTime();
> >
> > auto duration = endttime - stattime;
> >
> > writeln("Hello Duration ==> ", duration);
> >
> > }
>
> I know John identified Stopwatch, but just an FYI, Duration has the
> method total: http://dlang.org/phobos/core_time.html#.Duration.total
>
> I think doing:
>
> writeln("Hello Duration ==> ", duration.total!"usecs");
>
> would also work.
Yes, you could do that, but doing timing with the realtime clock is
fundamentally wrong, because the clock can change on you while you're
timing. That's why using a monotonic clock is better, since it's guaranteed
to never move backwards. Unfortunately, while StopWatch does use a monotonic
clock, it currently does that by using TickDuration for that rather than
MonoTime, so its result is a TickDuration rather than a Duration, so it's a
bit harder to use than would be nice, but it is more correct to use
StopWatch than to subtract SysTimes. Alternatively, you could just use
MonoTime directly. e.g.
auto startTime = MonoTime.currTime;
// do stuff
auto endTime = MonoTime.currTime;
audo duration = endTime - startTime;
writeln("Hello Duration ==> ", duration.total!"usecs");
in which case you get a Duration just like with subtract SysTimes, and the
suggestion of using total works just fine.
I need to put together replacements for the benchmarking functions in
std.datetime (probably in std.benchmark) which use MonoTime and Duration
rather than TickDuration so that we can deprecate the ones in std.datetime
which use TickDuration (and deprecate TickDuration itself).
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list