Benchmarking Time Unit

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Nov 1 13:26:53 PDT 2016


On Tuesday, November 01, 2016 20:19:31 Nordlöw via Digitalmars-d-learn 
wrote:
> On Tuesday, 1 November 2016 at 18:39:01 UTC, Jonathan M Davis
>
> wrote:
> > Alternatively, you can always do something like
> >
> > immutable before = MonoTime.currTime();
> > // do stuf...
> > immutable after = MonoTime.currTime();
> > Duration timeSpent = after - before;
>
> MonoTime has about 5-10 % fluctuations on my laptop. Is this as
> good as it gets?

How precise it is depends on your system. MonoTime uses the system's
monotonic clock at whatever precision it has. And even if it's perfectly
precise, you're going to see fluctuations in how long it takes your code to
run, since it's not the only thing running on your computer. That's part of
why std.datetime.benchmark runs the function over and over again rather
than only running it only once. That way, you get the average performance
rather than the performance for a single run.

- Jonathan m Davis




More information about the Digitalmars-d-learn mailing list