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

Eugen picture Eugen · Apr 26, 2012 · Viewed 19.4k times · Source

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.

Answer

Vincent Hiribarren picture Vincent Hiribarren · Apr 26, 2012

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.