|
class | StopWatch |
| High-resolution stopwatch timer for precise event timing and performance measurement. More...
|
|
#include< stopwatch.h>
The StopWatch class provides high-resolution timing capabilities for measuring code execution time, profiling performance, and timing events with precision beyond the standard RTOS tick resolution.
Features
- High-resolution timing using hardware timers
- Start/Stop functionality with cumulative timing
- Clear and reset capabilities
- Time reading without stopping the timer
- Platform-independent API with automatic timer allocation
Basic Usage
for (volatile int i = 0; i < numLoop; i++)
{
count++;
}
unsigned long long elapsed = myStopwatch.
GetTime();
double seconds = myStopwatch.
Convert(elapsed);
printf("Elapsed time: %f seconds\n", seconds);
High-resolution stopwatch timer for precise event timing and performance measurement.
Definition stopwatch.h:138
double Convert(unsigned long long ticks)
Convert timer ticks to seconds.
unsigned long long GetTime()
Get the current elapsed time without stopping the timer.
void Start(bool clear_count=false)
Start or resume the stopwatch timer.
unsigned long long Stop()
Stop the stopwatch and return elapsed time.
Advanced Usage - Cumulative Timing
timer.Start();
DoOperation1();
timer.Stop();
timer.Start();
DoOperation2();
timer.Stop();
double totalTime = timer.Convert(timer.GetTime());
printf("Total time for both operations: %f seconds\n", totalTime);
Timing Without Stopping
timer.Start();
while (processing)
{
DoWork();
unsigned long long current = timer.GetTime();
if (current > MAX_ALLOWED_TIME)
{
printf("Warning: Operation taking too long!\n");
break;
}
}
timer.Stop();
Timer Resolution
double resolution = timer.CountResolution();
printf("Timer resolution: %f seconds per tick\n", resolution);
- Note
- On platforms with dedicated RTOS timers, the timer_number parameter in the constructor can be omitted and the system will automatically allocate an appropriate timer.
-
The StopWatch maintains cumulative time across Start/Stop cycles unless explicitly cleared with Clear() or Start(true).
- See also
- Interval Timer For periodic timing and callbacks
-
High Resolution Delay Timer For precise delays
-
OSTimeDly() For RTOS-based delays