Executing Time Functions
The timing functions can be used to obtain execution time of a section of code to aid in analysis and optimization.
Please note that the timing functions may use a different timer than the default kernel timer, where the timer being used is specified by architecture, SoC or board configuration.
Configuration
To allow using the timing functions, CONFIG_TIMING_FUNCTIONS
needs to be enabled.
Usage
To gather timing information:
Call
timing_init()
to initialize the timer.Call
timing_start()
to signal the start of gathering of timing information. This usually starts the timer.Call
timing_counter_get()
to mark the start of code execution.Call
timing_counter_get()
to mark the end of code execution.Call
timing_cycles_get()
to get the number of timer cycles between start and end of code execution.Call
timing_cycles_to_ns()
with total number of cycles to convert number of cycles to nanoseconds.Repeat from step 3 to gather timing information for other blocks of code.
Call
timing_stop()
to signal the end of gathering of timing information. This usually stops the timer.
Example
This shows an example on how to use the timing functions:
#include <zephyr/timing/timing.h>
void gather_timing(void)
{
timing_t start_time, end_time;
uint64_t total_cycles;
uint64_t total_ns;
timing_init();
timing_start();
start_time = timing_counter_get();
code_execution_to_be_measured();
end_time = timing_counter_get();
total_cycles = timing_cycles_get(&start_time, &end_time);
total_ns = timing_cycles_to_ns(total_cycles);
timing_stop();
}