DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
Stopwatch.cpp
Go to the documentation of this file.
1#include "pch.h"
3
4#if defined(_WIN32)
5#include <windows.h>
6#else
7#include <chrono>
8#endif
9
10namespace DotNetDupe {
11 namespace System {
12 namespace Diagnostics {
13
14 const long long Stopwatch::Frequency = []() {
15#if defined(_WIN32)
16 LARGE_INTEGER li;
17 QueryPerformanceFrequency(&li);
18 return li.QuadPart;
19#else
20 return 1000000000LL; // Nanoseconds
21#endif
22 }();
23
24 const bool Stopwatch::IsHighResolution = true;
25
26 struct Stopwatch::Impl {
27 TimeProviderPtr timeProvider;
28 long long elapsedTicks;
29 long long startTimeStamp;
30 bool isRunning;
31 };
32
34
36 : m_pImpl(SmartPointer<Impl>::NewShared()) {
37 m_pImpl->timeProvider = timeProvider;
38 m_pImpl->elapsedTicks = 0;
39 m_pImpl->startTimeStamp = 0;
40 m_pImpl->isRunning = false;
41 }
42
43 Stopwatch::~Stopwatch() = default;
44
47 if (!m_pImpl->isRunning) {
48 m_pImpl->startTimeStamp = m_pImpl->timeProvider->GetTimestamp();
49 m_pImpl->isRunning = true;
50 }
51 }
52
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;
59 }
60 }
61
64 m_pImpl->elapsedTicks = 0;
65 m_pImpl->isRunning = false;
66 m_pImpl->startTimeStamp = 0;
67 }
68
71 m_pImpl->elapsedTicks = 0;
72 m_pImpl->startTimeStamp = m_pImpl->timeProvider->GetTimestamp();
73 m_pImpl->isRunning = true;
74 }
75
76 bool Stopwatch::IsRunning() const {
77 return m_pImpl->isRunning;
78 }
79
82 return m_pImpl->timeProvider->GetElapsedTime(0, GetRawElapsedTicks());
83 }
84
87 return (long long)Elapsed().GetTotalMilliseconds();
88 }
89
90 long long Stopwatch::ElapsedTicks() const {
92 return (long long)Elapsed().GetTicks();
93 }
94
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);
100 }
101 return elapsed;
102 }
103
106 Stopwatch sw;
107 sw.Start();
108 return sw;
109 }
110
113 Stopwatch sw(timeProvider);
114 sw.Start();
115 return sw;
116 }
117
120#if defined(_WIN32)
121 LARGE_INTEGER li;
122 QueryPerformanceCounter(&li);
123 return li.QuadPart;
124#else
125 auto now = std::chrono::steady_clock::now();
126 return std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()).count();
127#endif
128 }
129 }
130 }
131}
bool IsRunning() const
Gets a value indicating whether the Stopwatch timer is running.
Definition Stopwatch.cpp:76
TimeSpan Elapsed() const
Gets the total elapsed time measured by the current instance.
Definition Stopwatch.cpp:80
long long ElapsedMilliseconds() const
Gets the total elapsed time measured by the current instance, in milliseconds.
Definition Stopwatch.cpp:85
long long ElapsedTicks() const
Gets the total elapsed time measured by the current instance, in timer ticks.
Definition Stopwatch.cpp:90
static const bool IsHighResolution
Indicates whether the timer is based on a high-resolution performance counter.
Definition Stopwatch.h:78
void Reset()
Stops time measurement and resets the elapsed time to zero.
Definition Stopwatch.cpp:62
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.
Definition Stopwatch.h:75
void Restart()
Stops time measurement, resets the elapsed time to zero, and starts measuring elapsed time.
Definition Stopwatch.cpp:69
static long long GetTimestamp()
Gets the current number of ticks in the timer mechanism.
void Stop()
Stops measuring elapsed time for an interval.
Definition Stopwatch.cpp:53
~Stopwatch() override
Destructor releasing stopwatch resources.
Stopwatch()
Initializes a new instance of the Stopwatch class.
Definition Stopwatch.cpp:33
void Start()
Starts, or resumes, measuring elapsed time for an interval.
Definition Stopwatch.cpp:45
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...
Definition TimeSpan.h:20
double GetTotalMilliseconds() const
Gets the value of the current TimeSpan expressed in whole and fractional milliseconds.
Definition TimeSpan.h:50
int64_t GetTicks() const
Gets the number of ticks that represent the value of the current TimeSpan structure.
Definition TimeSpan.h:46
SmartPointer< TimeProvider > TimeProviderPtr
Definition Stopwatch.h:12