CSimTimer – High Precision Timer Class
The CSimTimer class provides high-precision timing functionality using
std::chrono::steady_clock to ensure monotonic and accurate time measurements.
It can measure elapsed time, pause/resume, and handle delayed timers.
Key Features
- SetMark() – Start or reset a timing mark.
- GetElapsedMilliTimeFromMark() – Returns milliseconds elapsed since the last mark.
- GetElapsedMicroTimeFromMark() – Returns microseconds elapsed since the last mark.
- GetSystemTime(), GetMilliTime(), GetMicroTime(), GetNanoTime() – Returns the current system time in various units.
- StartTimer(delay) – Starts a countdown timer with a given delay in milliseconds.
- IsTimeOver() – Checks if the countdown has finished.
- pause() / resume() – Pauses or resumes the timer without losing elapsed time.
- StopTimer() – Stops the timer immediately.
Usage Example
// Create a timer
CSimTimer timer;
// Start the timer
timer.StartTimer(5000); // 5-second timer
// Perform some work...
while (!timer.IsTimeOver()) {
// do work
}
// Check elapsed time since last mark
timer.SetMark();
// ...some operations...
auto elapsedMs = timer.GetElapsedMilliTimeFromMark();
This class is suitable for simulation loops, profiling, and time-sensitive operations, providing both high-resolution and robust timing guarantees across platforms.