Clock class

A class used for measuring time.

It supports multiple precisions from the enum Clock::Mode: minutes, seconds, milliseconds, microseconds, nanoseconds, ticks and processor time stamp.

In order to use a clock, first you have to start it with the Clock::Start() method. Then just use the Clock::GetElapsed() method (or its variants) to get the elapsed time since the clock was started.

You can pause the clock using Clock::Pause(), then use Start() again to continue counting the accumulated time. If you want to clear the accumulated time and count from 0, use Clock::Reset().

Public types

enum class Mode { Ticks = 0, ProcessorTimeStamp = 1, Nanoseconds = 2, Microseconds = 3, Milliseconds = 4, Seconds = 5, Minutes = 6 }

Constructors, destructors, conversion operators

Clock(Clock::Mode mode = Clock::Mode::Ticks, bool start = false)
Creates a new clock object with the specified precision mode.

Public functions

auto SetMode(Clock::Mode mode) -> void
Changes the precision mode.
auto Start() -> void
Starts the clock, if it's not already running.
auto Pause() -> void
Stops the clock, reseting the initial time to 0.
auto Reset() -> void
Stops the clock and clears all accumulated time.
auto GetElapsed() const -> float
Returns the elapsed time in the specified precision measure, conserving all decimals.
auto GetElapsedTicks() const -> LARGE_INTEGER
Returns the elapsed time in ticks (regardles of the clock mode).
auto GetElapsedTime() const -> int
Returns the elapsed time in the specified precision measure, rounded to the closest integer.
auto IsRunning() const -> bool
Tells whether the clock is counting (true) or is paused (false).

Protected variables

LARGE_INTEGER mStartTime
LARGE_INTEGER mAccumulatedTime
Clock::Mode mMode
The mode (precision) with which the time is measured.
float mMeasurePerTick
Derived from mMode, how many units of measurement (seconds, nanoseconds, etc) are equal to 1 tick.

Enum documentation

enum class Clock::Mode

Enumerators
Ticks

Uses QueryPerformanceCounter to measure time (in ticks).

ProcessorTimeStamp

Uses __rdtsc (Read Time Stamp Counter) to get the current time (in ticks?), instead of QueryPerformanceCounter.

Nanoseconds

Measures the time with nanosecond precision (1 second = 10e9 nanoseconds).

Uses QueryPerformanceCounter to measure time.

Microseconds

Measures the time with microsecond precision (1 second = 10e6 microseconds).

Uses QueryPerformanceCounter to measure time.

Milliseconds

Measures the time with milliseconds precision (1 second = 10e3 milliseconds).

Uses QueryPerformanceCounter to measure time.

Seconds

Measures the time with seconds precision.

Uses QueryPerformanceCounter to measure time.

Minutes

Measures the time with minute precision (60 seconds = 1 minute).

Uses QueryPerformanceCounter to measure time.

Function documentation

Clock::Clock(Clock::Mode mode = Clock::Mode::Ticks, bool start = false)

Creates a new clock object with the specified precision mode.

Parameters
mode The precision with which the clock will measure time.
start [Optional] If true, the clock will start counting time. False by default.

Optionally, you can make the clock start counting.

void Clock::SetMode(Clock::Mode mode)

Changes the precision mode.

Parameters
mode The precision with which the clock will measure time.

void Clock::Start()

Starts the clock, if it's not already running.

If the clock was paused, it will continue counting from the time accumulated until the last pause.

void Clock::Pause()

Stops the clock, reseting the initial time to 0.

After pausing, the clock can be started again using Clock::Start(); if started again, it will continue with the time accumulated until the stop.

void Clock::Reset()

Stops the clock and clears all accumulated time.

This is equivalent to creating a new clock again. After a reset, getting the elapsed time will return 0 until the clock is started again.

float Clock::GetElapsed() const

Returns the elapsed time in the specified precision measure, conserving all decimals.

Returns The time passed since the clock was created/reset, in the assigned units.