Struct std.datetime.StopWatch
The old benchmarking functionality in std.datetime (which uses
TickDuration
) has been deprecated. Use what's in
std.datetime.stopwatch instead. It uses MonoTime
and
Duration
. See
StopWatch
. This symbol will be removed
from the documentation in October 2018 and fully removed from Phobos
in October 2019.
struct StopWatch
;
StopWatch
measures time as precisely as possible.
This class uses a high-performance counter. On Windows systems, it uses
QueryPerformanceCounter
, and on Posix systems, it uses
clock_gettime
if available, and gettimeofday
otherwise.
But the precision of StopWatch
differs from system to system. It is
impossible to for it to be the same from system to system since the precision
of the system clock varies from system to system, and other system-dependent
and situation-dependent stuff (such as the overhead of a context switch
between threads) can also affect StopWatch
's accuracy.
Constructors
Name | Description |
---|---|
this
|
Auto start with constructor. |
Properties
Name | Type | Description |
---|---|---|
running [get]
|
bool | Confirm whether this stopwatch is measuring time. |
Methods
Name | Description |
---|---|
opEquals
|
|
peek
|
Peek at the amount of time which has passed since the stop watch was started. |
reset
|
Resets the stop watch. |
setMeasured
|
Set the amount of time which has been measured since the stop watch was started. |
start
|
Starts the stop watch. |
stop
|
Stops the stop watch. |
Example
void writeln(S...)(S args){}
static void bar() {}
StopWatch sw;
enum n = 100;
TickDuration[n] times;
TickDuration last = TickDuration .from!"seconds"(0);
foreach (i; 0 .. n)
{
sw .start(); //start/resume mesuring.
foreach (unused; 0 .. 1_000_000)
bar();
sw .stop(); //stop/pause measuring.
//Return value of peek() after having stopped are the always same.
writeln((i + 1) * 1_000_000, " times done, lap time: ",
sw .peek() .msecs, "[ms]");
times[i] = sw .peek() - last;
last = sw .peek();
}
real sum = 0;
// To get the number of seconds,
// use properties of TickDuration.
// (seconds, msecs, usecs, hnsecs)
foreach (t; times)
sum += t .hnsecs;
writeln("Average time: ", sum/n, " hnsecs");
Authors
Jonathan M Davis and Kato Shoichi