View source code
Display the source code in core/time.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

core.time.MonoTimeImpl.opBinary - multiple declarations

Function MonoTimeImpl.opBinary

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

Duration opBinary(string op) (
  MonoTimeImpl rhs
) const pure nothrow @nogc
if (op == "-");

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

MonoTime before = MonoTime.currTime;
// do stuff
MonoTime after = MonoTime.currTime;

// How long it took.
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

auto before = MonoTime.currTime;
// do stuff
auto after = MonoTime.currTime;
auto timeElapsed = after - before;
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.

Function MonoTimeImpl.opBinary

Adding or subtracting a Duration to/from a MonoTime results in a MonoTime which is adjusted by that amount.

MonoTimeImpl opBinary(string op) (
  Duration rhs
) const pure nothrow @nogc
if (op == "+" || op == "-");

Authors

Jonathan M Davis and Kato Shoichi

License

Boost License 1.0.