dip::testing::Timer class

A timer object to help time algorithm execution.

The methods GetCpu and GetWall return the CPU and wall time, respectively, in seconds that passed in between object creation and the last call to Stop. Stop does not actually stop the timer, it just records the time it was last called. Reset resets the timer, as if it had just been created.

dip::Timer timer;
// do some computation
timer.Stop();
std::cout << "Computation 1: Wall time = " << timer.GetWall() << " s. CPU time = " << timer.GetCpu() << " s.\n";
timer.Reset();
// do some other computation
timer.Stop();
std::cout << "Computation 2: Wall time = " << timer.GetWall() << " s. CPU time = " << timer.GetCpu() << " s.\n";

Note that it is also possible to directly put the timer object to the output stream:

dip::Timer timer;
// do some computation
timer.Stop();
std::cout << "Computation 1: " << timer << '\n';

The stream output reports both the wall time and the CPU time, and uses meaningful units (minutes, seconds, milliseconds or microseconds).

Wall time is the real-world time that elapsed. CPU time is the time that the CPU spent working for the current program. These differ in two ways: CPU time might pass slower if the program has to share resources with other running programs; and CPU time might pass faster if there are multiple CPUs (or cores) working for the same program. The latter case means that, on a multi-threaded environment, CPU time is the sum of times for each of the executed threads.

Wall time is obtained through std::chrono::steady_clock, and CPU time through std::clock. This object does not do anything special with these standard library routines, except for providing a simpler interface.

Constructors, destructors, assignment and conversion operators

Timer()
The default-constructed object records its creation time as the start time for the timer.

Functions

void Reset()
Records the current time as the start time for the timer.
void Stop()
Records the current time as the stop time for the timer.
auto GetCpu() const -> dip::dfloat
Returns the CPU time in seconds elapsed in between the creation of the timer (or the last call to Reset) and the last call to Stop.
auto GetWall() const -> dip::dfloat
Returns the wall time in seconds elapsed in between the creation of the timer (or the last call to Reset) and the last call to Stop.
static auto CpuResolution() -> dip::dfloat
Returns the number of clock ticks per second for the CPU clock.
static auto WallResolution() -> dip::dfloat
Returns the number of clock ticks per second for the wall clock.