17 QueryPerformanceFrequency(&li);
26 struct Stopwatch::Impl {
28 long long elapsedTicks;
29 long long startTimeStamp;
37 m_pImpl->timeProvider = timeProvider;
38 m_pImpl->elapsedTicks = 0;
39 m_pImpl->startTimeStamp = 0;
40 m_pImpl->isRunning =
false;
47 if (!m_pImpl->isRunning) {
48 m_pImpl->startTimeStamp = m_pImpl->timeProvider->GetTimestamp();
49 m_pImpl->isRunning =
true;
55 if (m_pImpl->isRunning) {
56 long long endTimeStamp = m_pImpl->timeProvider->GetTimestamp();
57 m_pImpl->elapsedTicks += (endTimeStamp - m_pImpl->startTimeStamp);
58 m_pImpl->isRunning =
false;
64 m_pImpl->elapsedTicks = 0;
65 m_pImpl->isRunning =
false;
66 m_pImpl->startTimeStamp = 0;
71 m_pImpl->elapsedTicks = 0;
72 m_pImpl->startTimeStamp = m_pImpl->timeProvider->
GetTimestamp();
73 m_pImpl->isRunning =
true;
77 return m_pImpl->isRunning;
82 return m_pImpl->timeProvider->GetElapsedTime(0, GetRawElapsedTicks());
95 long long Stopwatch::GetRawElapsedTicks()
const {
97 long long elapsed = m_pImpl->elapsedTicks;
98 if (m_pImpl->isRunning) {
99 elapsed += (m_pImpl->timeProvider->
GetTimestamp() - m_pImpl->startTimeStamp);
122 QueryPerformanceCounter(&li);
125 auto now = std::chrono::steady_clock::now();
126 return std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()).count();
bool IsRunning() const
Gets a value indicating whether the Stopwatch timer is running.
TimeSpan Elapsed() const
Gets the total elapsed time measured by the current instance.
long long ElapsedMilliseconds() const
Gets the total elapsed time measured by the current instance, in milliseconds.
long long ElapsedTicks() const
Gets the total elapsed time measured by the current instance, in timer ticks.
static const bool IsHighResolution
Indicates whether the timer is based on a high-resolution performance counter.
void Reset()
Stops time measurement and resets the elapsed time to zero.
static Stopwatch StartNew()
Initializes a new Stopwatch instance, sets the elapsed time property to zero, and starts measuring el...
static const long long Frequency
Gets the frequency of the timer as the number of ticks per second.
void Restart()
Stops time measurement, resets the elapsed time to zero, and starts measuring elapsed time.
static long long GetTimestamp()
Gets the current number of ticks in the timer mechanism.
void Stop()
Stops measuring elapsed time for an interval.
~Stopwatch() override
Destructor releasing stopwatch resources.
Stopwatch()
Initializes a new instance of the Stopwatch class.
void Start()
Starts, or resumes, measuring elapsed time for an interval.
A unified smart pointer that supports both unique and shared ownership semantics.
An abstraction that provides time-related functions to enable testability of time-dependent code.
virtual int64_t GetTimestamp() const =0
Gets the current high-frequency counter value in ticks.
Represents a time interval (duration of time or elapsed time) measured as a positive or negative numb...
double GetTotalMilliseconds() const
Gets the value of the current TimeSpan expressed in whole and fractional milliseconds.
int64_t GetTicks() const
Gets the number of ticks that represent the value of the current TimeSpan structure.
SmartPointer< TimeProvider > TimeProviderPtr