This guide walks through using the CSimTimer class to add high-precision, monotonic timing to your simulation code, including elapsed-time measurement, countdowns, and pause/resume control.
1. Understand what CSimTimer gives you
CSimTimer is built on std::chrono::steady_clock, which guarantees monotonic, accurate time measurement (it will not jump backward if the system clock changes). Use it for simulation loops, profiling, and any time-sensitive operation.
2. Know the available methods
| Method | Description |
|---|---|
SetMark() | Starts or resets a timing mark. |
GetElapsedMilliTimeFromMark() | Returns milliseconds elapsed since the last mark. |
GetElapsedMicroTimeFromMark() | Returns microseconds elapsed since the last mark. |
GetSystemTime(), GetMilliTime(), GetMicroTime(), GetNanoTime() | Return 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. |
3. Run a countdown timer
- Create a
CSimTimerinstance. - Call
StartTimer(delay)with the delay in milliseconds. - Poll
IsTimeOver()in your loop to check whether the countdown has elapsed.
4. Measure elapsed time from a mark
- Call
SetMark()to start or reset the reference point. - Perform the operations you want to time.
- Call
GetElapsedMilliTimeFromMark()(or the micro variant) to get the elapsed duration.
5. Full 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();
6. Pause and resume without losing time
Call pause() to suspend timing and resume() to continue from where it left off, without resetting the elapsed time. Use StopTimer() when you need to end the timer immediately instead of pausing it.
CSimTimer is designed for simulation loops, profiling, and time-sensitive operations, providing both high-resolution and robust timing guarantees across platforms.