Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 367
Eugen Avatar asked Apr 26 '12 10:04

Eugen


People also ask

How fast does Arduino execute code?

How fast does it execute the loop? It depends on which Arduino board you're using, but an Arduino Uno has a clock speed of 16 megahertz. So that means that 16 million instructions are happening every second on the Arduino! Each line of code isn't necessarily one instruction.

What is duration in Arduino?

This is a control timer to turn on a connected device for a preset length of time. Set in program for 30, 60, 90 or 120 minutes.

Which function can be used to measure the time between two events?

Explanation: the clock is an instrument which is used to measure the time interval between the occurrences of two different events.

How long is Arduino loop?

loop() is not timed. The number of loops in a second is equal to 16000000 divided by the number of processor cycles your loop() method takes - if the loop() is empty, it will run at 16MHz, whereas if it has 32000000 processor cycles it will run at 0.5Hz.


1 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.

like image 183
Vincent Hiribarren Avatar answered Sep 18 '22 10:09

Vincent Hiribarren