How to measure the speed of an Arduino function's execution?

I need to determine the speed with which Arduino executes a certain function.

What would be the best time to do that? So far I found something with a Stopwatch class, but I'm wondering if there's any native method to do that.

Answers


A straightforward way is to use the millis() or micros() function in the Arduino library. You will have a finer grain result with micros().

For instance:

unsigned long start = micros();
// Call to your function
myFunction();
// Compute the time it took
unsigned long end = micros();
unsigned long delta = end - start;
Serial.println(delta)

Read carefully the documentation of micros(): there are some information about the time resolution.


the least intrusive way is to set a port pin high before the function call and low afterwards. Put an oscilloscope on the port pin and measure the high-time.

This can also give you a good qualitative idea of the variability of execution time by triggering on the rising edge and watching the jitter on the falling edge.


Need Your Help

How to permanently change a variable in a Python game loop

python variables blender

I have this script in a game that I am making. This will be used in the Blender game engine. Blender runs scripts over and over from top to bottom continuously, so if I declare a variable at the

method returning more length then the properties

javascript methods

i have a code snippet below but i dont know why the condition is not true??