std.datetime.stopwatch

Module containing some basic benchmarking and timing functionality.

For convenience, this module publicly imports core.time.

Unlike the other modules in std.datetime, this module is not currently

publicly imported in std.datetime.package, because the old versions of this functionality which use

TickDuration are in std.datetime.package and would

conflict with the symbols in this module. After the old symbols have gone through the deprecation cycle and have been fully removed, then this module will be publicly imported in std.datetime.package. The old, deprecated symbols has been removed from the documentation in December 2019 and currently scheduled to be fully removed from Phobos after 2.094.

So, for now, when using std.datetime.stopwatch, if other modules from std.datetime are needed, then either import them individually rather than importing std.datetime, or use selective or static imports to import std.datetime.stopwatch. e.g.

---------------------------------------------------------------------------- import std.datetime; import std.datetime.stopwatch : benchmark, StopWatch; ----------------------------------------------------------------------------

The compiler will then know to use the symbols from std.datetime.stopwatch rather than the deprecated ones from std.datetime.package.

License

Types 2

aliasAutoStart = Flag!"autoStart"

Used by StopWatch to indicate whether it should start immediately upon construction.

If set to AutoStart.no, then the StopWatch is not started when it is constructed.

Otherwise, if set to AutoStart.yes, then the StopWatch is started when it is constructed.

structStopWatch

StopWatch is used to measure time just like one would do with a physical stopwatch, including stopping, restarting, and/or resetting it.

MonoTime is used to hold the time, and it uses the system's

monotonic clock, which is high precision and never counts backwards (unlike the wall clock time, which can count backwards, which is why

SysTime should not be used for timing).

Note that the precision of StopWatch differs from system to system. It is impossible for it to be the same for all systems, since the precision of the system clock and other system-dependent and situation-dependent factors (such as the overhead of a context switch between threads) varies from system to system and can affect StopWatch's accuracy.

Fields
bool _running
MonoTime _timeStarted
long _ticksElapsed
Methods
void reset() @safe nothrow @nogcResets the StopWatch.
void start() @safe nothrow @nogcStarts the StopWatch.
void stop() @safe nothrow @nogcStops the StopWatch.
Duration peek() @safe const nothrow @nogcPeek at the amount of time that the StopWatch has been running.
void setTimeElapsed(Duration timeElapsed) @safe nothrow @nogcSets the total time which the StopWatch has been running (i.e. what peek returns).
bool running() @property @safe const pure nothrow @nogcReturns whether this StopWatch is currently running.
Constructors
this(AutoStart autostart)Constructs a StopWatch. Whether it starts immediately depends on the AutoStart argument.

Functions 1

fnDuration[fun.length] benchmark(fun...)(uint n)Benchmarks code for speed assessment and comparison.