Calculating time spent by a function in Microcontroller Applications

I would like to write a simple profiler for my STM32F100VE microcontroller. I have seen that I can include time.h and based on the c standard functions I can use it, but I was confused about _CLOCKS_PER_SEC_ 1000. I'm not sure if that approach will work or not.

How would one try measure the time a function took in micro-controller application?

• Do you need to know how much time the function needs to be executed, or do you need to know how much time the mcu spends in the function, ie how often it's called? Apr 12, 2014 at 15:47
• @VladimirCravero I want to know how much time the mcu spends in that function. Basically I'm running a game loop, and would like to know how much that function "ate" from that game loop which is sync to 30FPS. Apr 12, 2014 at 15:59
• One method I've used for verification of design is to toggle a port pin, you can then look at it on an oscilloscope. Usually I know what it's supposed to be from simulation. Apr 12, 2014 at 16:05
• Spehro idea is great, I used to do that too. Just assert a pin when you enter the function and deassert it when you exit. You can check how many times it's called, how long does it take to return and so on. Keep in mind: is the function interruptible? Apr 12, 2014 at 16:27
• Lacking an oscilloscope you could put and RC filter on the toggle pin and see what the (averaged) output is with a multimeter. If you know the loop frequency you can calculate back to the (average!) time spend in the routine. Apr 12, 2014 at 18:02

3 Answers

The most basic maneuver for complicated functions is to utilize the onboard timers.

Depending on how you set them up, you can get them to count exactly the number of clock cycles it takes for a certain function to execute. Simply read the timer before the function call and then again after the function call.

You will have to look through the assembly code to know how much time to take off the timer for the read calls but if it is a lengthy function or if you don't mind being by a couple of clock cycles then it is ok.

The only error in the conversion from clock cycles back to time is the possible error in the system clock's oscillating frequency. Again, this will be negligible for functions which take significant time to execute.

It sounds like in your case you want something around 30Hz (30 FPS) which is a very long time compared time cycle of the processor clock.

• Good answer, I just want to add that if all of the code runs from the same system clock then the exact clock frequency probably doesn't matter in this case. It's usually the number of clock cycles required to execute a function that is of most interest. Apr 12, 2014 at 17:59
• @MathEE, can you explain the approach of starting the timer and stopping it ? I would start the timer to count, then call my function then stop it, the timer's value would be proportional the time spent in the function? but how would I convert the value from the timer to milliseconds that are spent ? Apr 12, 2014 at 19:01
• You can either start it and stop it or you can just keep one running then read off the values (permitted it doesn't overflow). The system clock has a frequency $F_{CY}$ and lets say you set your timer to increment at each system clock tick then the elapsed time is $(\text{ change in timer})/F_{CY}$. Apr 12, 2014 at 21:45

If it is a small function you could just look at the disassembly and count the instructions.

• Unfortunately, I do not have an ICD. Apr 12, 2014 at 16:04
• You can look at the generated assembler without an ICD. Which tools are you using? Also is it a small function?
– Will
Apr 12, 2014 at 16:05
• It's a triangle rasterization function, with calls to LCD framebuffer to write pixels there. Apr 12, 2014 at 16:07
• @Will Your short answer has valuable insight, but it would work better as a comment. You have enough reputation to post comments. Apr 12, 2014 at 16:12
• Some inexpensive hand-held meters can measure frequency and duty cycle. F/ex: TekPower TP4000ZC. (Note that one user claimed this one's impedance wasn't very high. I'm happy enough with mine...) Apr 12, 2014 at 16:56

Do you need to use that information in your software as well, or do you only need it for debugging/development purposes? If it's the second one (I assume it is, as your main goal is profiling), just toggling an output at the beginning and the end of the function, and measuring the time with an oscilloscope might be the quickest, easiest and most accurate measurement.

• This is what I do all the time. Much simpler than setting up another timer. Apr 13, 2014 at 8:05
• While using a scope might be the quickest and easiest I don't think it is the most accurate. Using the on-chip timers allows you to exercise the function over a broad set of input conditions over a long period of time. You can then find the maximum execution time, in actual clock cycles. For code that has significant variability it would be hard to catch that on a scope. Apr 13, 2014 at 12:06
• @JoeHass : a lot of modern scopes have a feature called "persistence mode", where successive signals are superimposed.
– vsz
Apr 13, 2014 at 17:21