NetBurner 3.5.6
PDF Version |
High-resolution stopwatch timer for precise event timing and performance measurement. More...
#include <stopwatch.h>
Public Member Functions | |
StopWatch (int timer_number=FIRST_UNUSED_TIMER) | |
Construct a stopwatch object with optional timer selection. | |
void | Start (bool clear_count=false) |
Start or resume the stopwatch timer. | |
void | Clear () |
Clear the accumulated elapsed time. | |
unsigned long long | Stop () |
Stop the stopwatch and return elapsed time. | |
unsigned long long | GetTime () |
Get the current elapsed time without stopping the timer. | |
double | CountResolution () |
Get the resolution of a single timer tick in seconds. | |
double | Convert (unsigned long long ticks) |
Convert timer ticks to seconds. | |
High-resolution stopwatch timer for precise event timing and performance measurement.
The StopWatch class provides a hardware-based timer interface for measuring time intervals with resolution far exceeding the RTOS tick rate. It is ideal for performance profiling, timeout detection, and precise event timing.
The stopwatch operates in two states:
Time accumulates across multiple Start/Stop cycles unless explicitly cleared, making it useful for measuring cumulative execution time across multiple code sections or function calls.
StopWatch objects are not inherently thread-safe. If multiple tasks need to access the same StopWatch, appropriate synchronization (mutexes, semaphores) must be used to prevent race conditions.
On most platforms, hardware timers are automatically allocated. Some older platforms may require explicit timer number specification via the constructor.
StopWatch::StopWatch | ( | int | timer_number = FIRST_UNUSED_TIMER | ) |
Construct a stopwatch object with optional timer selection.
Creates a new StopWatch instance and allocates a hardware timer for timing operations. On modern platforms with dedicated RTOS timers, the timer_number parameter can be omitted and the system will automatically select an available timer resource.
The stopwatch is created in a stopped state with zero elapsed time. Call Start() to begin timing.
timer_number | Optional timer resource identifier. Use FIRST_UNUSED_TIMER (default) for automatic allocation, or specify a specific timer number if your platform requires explicit timer selection. Valid timer numbers are platform-dependent. |
Example:
void StopWatch::Clear | ( | ) |
Clear the accumulated elapsed time.
Resets the stopwatch's elapsed time counter to zero. If the stopwatch is currently running, it continues running but from a zero starting point. If stopped, the elapsed time is simply cleared.
This is equivalent to calling Start(true) but works whether the stopwatch is running or stopped.
Example:
double StopWatch::Convert | ( | unsigned long long | ticks | ) |
Convert timer ticks to seconds.
Converts a time interval or elapsed time value from hardware timer ticks (as returned by Stop() or GetTime()) to seconds as a double precision floating point number. This provides a human-readable representation of the measured time.
The conversion accounts for the platform's timer frequency and provides accurate time representation suitable for display, logging, or mathematical operations requiring time in standard units.
ticks | The time value in hardware timer ticks to convert. This is typically obtained from Stop() or GetTime(). |
Example - Basic conversion:
Example - Display in multiple units:
Example - Performance comparison:
Example - Throughput calculation:
double StopWatch::CountResolution | ( | ) |
Get the resolution of a single timer tick in seconds.
Returns the time value of one hardware timer tick, expressed as a double precision floating point number in seconds. This represents the finest granularity at which the stopwatch can measure time.
The resolution depends on the hardware timer frequency and typically ranges from nanoseconds to microseconds on NetBurner platforms. Higher resolution (smaller tick duration) provides more precise timing measurements.
Example - Display timer characteristics:
Example - Calculate measurement precision:
Example - Validate timing requirements:
unsigned long long StopWatch::GetTime | ( | ) |
Get the current elapsed time without stopping the timer.
Returns the current accumulated elapsed time in hardware timer ticks. Unlike Stop(), this function does not change the state of the stopwatch - if the timer is running, it continues running; if stopped, it remains stopped.
This is particularly useful for checking progress during long operations, implementing timeouts, or monitoring timing without interrupting the measurement.
Example - Check progress without stopping:
Example - Timeout implementation:
Example - Progress reporting:
void StopWatch::Start | ( | bool | clear_count = false | ) |
Start or resume the stopwatch timer.
Begins timing from the current moment. If the stopwatch was previously stopped, it will continue accumulating time from the last elapsed value unless clear_count is true. This allows for cumulative timing across multiple Start/Stop cycles.
If called when the stopwatch is already running, this function has no effect unless clear_count is true, in which case it restarts timing from zero.
clear_count | If true, resets the elapsed time to zero before starting. If false (default), continues accumulating time from the previous elapsed value, enabling cumulative timing. |
Example - Basic timing:
Example - Cumulative timing:
Example - Restart from zero:
unsigned long long StopWatch::Stop | ( | ) |
Stop the stopwatch and return elapsed time.
Stops the stopwatch timer and returns the total accumulated elapsed time in hardware timer ticks. The elapsed time is preserved and will continue to accumulate if Start() is called again without clearing.
If the stopwatch is already stopped, this function simply returns the current elapsed time without modifying the stopwatch state.
Example - Basic stop and convert:
Example - Multiple stop calls: