• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Measuring execution time of a function

123mmm

Member
Hello, I would like to ask the following question: if I write a program, in Arduino IDE, and I want to measure the execution time of some instruction(s), then it will be correct to measure the execution time by using a test variable, for example, let's say:

#define test_var = A2

setup(){
pinMode(test_var, OUTPUT);
digitalWrite(test_var, LOW);
}

loop(){
if ( condition ) {
digitalWrite(test_var, HIGH);
//instructions, etc, etc
digitalWrite(test_var, LOW);
}
}

And for measuring the execution time, I will connect the oscilloscope probe to the pin A2 and GND and I will measure the time test_var is high. It is correct ?
I am sorry if this has been asked before, I searched on the internet and found almost nothing about this, but probably I didn't used the correct search terms...
 

Nigel Goodwin

Super Moderator
Most Helpful Member
I've used the 'scope method' endless times over the decades - however, a MUCH easier way is to use the standard Arduino method of using the millis() function to do it's own timing, and print it out on the serial monitor.

There are countless examples out there, typical ones are those used in graphic speed tests for LCD drivers etc.

Basically store the millis() value to a variable, execute your function, and then store the millis() value to another variable, then simply subtract the second from the first, giving you the time taken in milliseconds.

I do the same on PIC's as well, using 1mS interrupts to create my own millis() function.
 

gophert

Well-Known Member
Most Helpful Member
Hello, I would like to ask the following question: if I write a program, in Arduino IDE, and I want to measure the execution time of some instruction(s), then it will be correct to measure the execution time by using a test variable, for example, let's say:

#define test_var = A2

setup(){
pinMode(test_var, OUTPUT);
digitalWrite(test_var, LOW);
}

loop(){
if ( condition ) {
digitalWrite(test_var, HIGH);
//instructions, etc, etc
digitalWrite(test_var, LOW);
}
}

And for measuring the execution time, I will connect the oscilloscope probe to the pin A2 and GND and I will measure the time test_var is high. It is correct ?
I am sorry if this has been asked before, I searched on the internet and found almost nothing about this, but probably I didn't used the correct search terms...
You can also use the Serial.print Command to send a character back to the serial monitor window. There is an option on the serial monitor window to display current time to about 8 decimal places so just subtract the times between two events.
 

Latest threads

EE World Online Articles

Loading
Top