Embedded Development: The Third Method for Measuring Code Execution Time

In engineering development, it is sometimes necessary to first confirm which part of the code consumes time in order to optimize the program. So, how do we measure code execution time? In previous articles, two methods were mentioned: using an oscilloscope to measure I/O level changes to calculate code execution time, and using the System Timer (STM) to measure code execution time. You can refer to the previous article “Debugging Techniques: Measuring Code Execution Time Using I/O”. Besides the above two methods, are there any other methods? Or is there a simpler method? Answer: Yes, this article introduces the third method—using Lauterbach’s Snooper function to measure code execution time.

1. Measuring Code Execution Time Using Lauterbach’s Snooper Function

Before discussing the measurement of code execution time using Lauterbach’s Snooper, let’s first compare it with STM.

(1) STM Measuring Code Execution Time

This article uses the wait() interface to wait for 1ms, representing the code to be measured, as shown below:

Embedded Development: The Third Method for Measuring Code Execution Time

Analysis: As shown in the code above, the frequency of STM is 100MHz (100.0e+6Hz). Plugging into the formula: t = 1 / Frq = 1/100*1000000 = 0.00000001s = 0.00001ms = 0.01us = 10ns, that is: 1 Tick = 10ns. ElapseTick = 100043*10 = 1000430ns = 1.00043ms, which is the actual code execution time to be tested for 1ms. Therefore, STM measures code execution time quite accurately.

(2) Measuring Code Execution Time Using Lauterbach’s Snooper Function

For more details on using Lauterbach’s Snooper function, you can refer to the previous article “TRACE32 Debugging: Basic Debugging Techniques of SystemMode and SNOOPer”. The test code is as follows:

uint32 CodeExc_StartTime = 0x00;uint32 CodeExc_EndTime = 0x00;static void timeout1(void *parameter){    CodeExc_StartTime++;    /* Wait for 1ms */    wait(100000);    CodeExc_EndTime++;}

Explanation:

Measuring the execution time of code using Lauterbach’s Snooper function is relatively simple; it only requires adding a variable (for example: CodeExc_StartTime and CodeExc_EndTime in this article) before and after the test code, and then checking the time interval of these two variables in Snooper to obtain the execution time of the target code.

1. Snooper Configuration In Snooper, set the Mode to “Changes“, which means: sample when data changes. The sampling rate (Rate) is set to 1us (highest sampling precision).

Embedded Development: The Third Method for Measuring Code Execution Time

2. Run the program to obtain the code execution time as shown in the figure below. Click “List” to get the time difference between CodeExc_StartTime and CodeExc_EndTime, as shown below:

Embedded Development: The Third Method for Measuring Code Execution Time

As shown in the figure above, the time difference between the two is between 980~1050us. Although there is a certain error compared to the set time of 1ms, it is sufficient for problem analysis.

Measurement Precautions:

1. The testing error during the startup phase is relatively large. It is recommended to measure code execution time using cumulative measurement parameters, which provides more accurate analysis for subsequent code, as illustrated below

Embedded Development: The Third Method for Measuring Code Execution Time

2. Compared to measurements using an oscilloscope and STM, this method has a certain error (approximately several microseconds). Assuming the CPU’s main frequency is 300MHz, it means that the execution time of a line of code is in the nanosecond (ns) range, while the maximum precision of the Snooper method is in the microsecond (us) range. Therefore, if the measured code execution time is in the us or ns range, this method is not suitable.

2. Comparison of Three Methods for Measuring Code Execution Time

The characteristics of measuring code execution time using an oscilloscope, STM, and Snooper are as follows:

Embedded Development: The Third Method for Measuring Code Execution Time

As shown, each of the three measurement methods has its own characteristics, and the appropriate solution can be selected based on the actual project. If the measured code or task execution time is in the ms range, using the Snooper method is simpler and faster. If the measured code is in the us or ns range, using an oscilloscope or STM is more appropriate.

Previous Highlights

Summary of previous excellent articles on Autosar: 1~70 Summary of previous excellent articles on Autosar: 71~100 Summary of previous excellent articles on Autosar: 101~150

Summary of previous excellent articles on Autosar: 151~200

Summary of previous excellent articles on Autosar: 201~251

UDS Snapshot: Differences and Connections Between Global Snapshot and Private Snapshot

CANoe Basics: Data Replay and Recording Operations

Communication: Cross-Core Reading of Signals, Occasional Inversion of Signal Values

INCA Basic Operations: Measurement Data Analysis

Monitoring Ethernet Data Interaction Based on VN5650 TAP Mode

Click below to follow and discuss Autosar/embedded topics. If needed, contact the author to join the group for more professional answers.

Leave a Comment