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?

  • \$\begingroup\$ 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? \$\endgroup\$ – Vladimir Cravero Apr 12 '14 at 15:47
  • \$\begingroup\$ @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. \$\endgroup\$ – Ahmed Saleh Apr 12 '14 at 15:59
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ – Spehro Pefhany Apr 12 '14 at 16:05
  • \$\begingroup\$ 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? \$\endgroup\$ – Vladimir Cravero Apr 12 '14 at 16:27
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Wouter van Ooijen Apr 12 '14 at 18:02

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.

  • \$\begingroup\$ 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. \$\endgroup\$ – Joe Hass Apr 12 '14 at 17:59
  • \$\begingroup\$ @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 ? \$\endgroup\$ – Ahmed Saleh Apr 12 '14 at 19:01
  • \$\begingroup\$ 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}\$. \$\endgroup\$ – SomeEE Apr 12 '14 at 21:45

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

  • \$\begingroup\$ Unfortunately, I do not have an ICD. \$\endgroup\$ – Ahmed Saleh Apr 12 '14 at 16:04
  • \$\begingroup\$ You can look at the generated assembler without an ICD. Which tools are you using? Also is it a small function? \$\endgroup\$ – Will Apr 12 '14 at 16:05
  • \$\begingroup\$ It's a triangle rasterization function, with calls to LCD framebuffer to write pixels there. \$\endgroup\$ – Ahmed Saleh Apr 12 '14 at 16:07
  • 2
    \$\begingroup\$ @Will Your short answer has valuable insight, but it would work better as a comment. You have enough reputation to post comments. \$\endgroup\$ – Nick Alexeev Apr 12 '14 at 16:12
  • \$\begingroup\$ 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...) \$\endgroup\$ – JRobert Apr 12 '14 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.

  • \$\begingroup\$ This is what I do all the time. Much simpler than setting up another timer. \$\endgroup\$ – tcrosley Apr 13 '14 at 8:05
  • \$\begingroup\$ 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. \$\endgroup\$ – Joe Hass Apr 13 '14 at 12:06
  • 1
    \$\begingroup\$ @JoeHass : a lot of modern scopes have a feature called "persistence mode", where successive signals are superimposed. \$\endgroup\$ – vsz Apr 13 '14 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.