MonoTimeImpl.opBinary

Subtracting two MonoTimes results in a Duration representing the amount of time which elapsed between them.

The primary way that programs should time how long something takes is to do

1 MonoTime before = MonoTime.currTime;
2 // do stuff
3 MonoTime after = MonoTime.currTime;
4 
5 // How long it took.
6 Duration timeElapsed = after - before;

or to use a wrapper (such as a stop watch type) which does that.

Warning: Because Duration is in hnsecs, whereas MonoTime is in system ticks, it's usually the case that this assertion will fail

1 auto before = MonoTime.currTime;
2 // do stuff
3 auto after = MonoTime.currTime;
4 auto timeElapsed = after - before;
5 assert(before + timeElapsed == after);

This is generally fine, and by its very nature, converting from system ticks to any type of seconds (hnsecs, nsecs, etc.) will introduce rounding errors, but if code needs to avoid any of the small rounding errors introduced by conversion, then it needs to use MonoTime's ticks property and keep all calculations in ticks rather than using Duration.

  1. Duration opBinary(MonoTimeImpl rhs)
    struct MonoTimeImpl(ClockType clockType)
    @safe
    opBinary
    const pure nothrow @nogc
    (
    string op
    )
    if (
    op == "-"
    )
  2. MonoTimeImpl opBinary(Duration rhs)
Suggestion Box / Bug Report