bembe24
bembe24

Reputation: 17

Arduino run time of a single line of code

How much time does it take Arduino run a single line of code? Or read an analog value from an analog pin?

I tried this code to read the analog read time:

void setup() {
  Serial.begin(9600);
  t1 = micros();
  val = analogRead(pin);
  t2 = micros();
  Serial.print(t2-t1);
}

It prints 208 microsec, but what I have read in the form sources that it takes 100 microsec to read an analog input. Is there something wrong with my code?

This code to read the execution time of a single line of code:

void setup() {
  Serial.begin(9600);
  t1 = micros();
  int x = 1 + 2;
  t2 = micros();
  Serial.print(t2-t1);
}

This shows 0 microsec. What is happening here? What am I doing wrong?

Upvotes: 0

Views: 2029

Answers (2)

Adwait Patil
Adwait Patil

Reputation: 171

Each line of code (called as statement in high level languages) does not take the same time to execute.

In extremely simplified language:

Each 'statement' is translated into number of machine code 'instructions' by the compiler. The number of instructions is dependent on the statement.

For example:

x = a + b;
y = p + q + r + s;

In the above example, the first statement requires fewer instructions than the second statement.

On a RISC processor (such as the one used in Arduino), each instruction will typically take a single instruction cycle. Thus, it can be safely assumed that every instruction takes the same amount of time to be executed.

Additional note:

val = analogRead(pin);

is a call to a function, which in turn will contains multiple statements which are converted into multiple instructions.

int x = 1 + 2;

is a simple statement, which will be converted into a number of instructions by the compiler.

I hope I have covered the relevant basics.

Upvotes: 0

Morgoth
Morgoth

Reputation: 5176

A line of Arduino code does not have a fixed execution time.

delay(1000); will take about a second to run, while Serial.print("Hello, World!"); might take a few hundred microseconds to run.

It depends on what the line does. A single line of Arduino code might get converted into 1 line or multiple lines of assembler code.

A simple for loop in Arduino code:

for (i=0; i<100; i++) {
    p[i] = i;
}

Might become this in assembler code:

LDI     R19,0x00
MOVW    R30,R24
ST      Z+,R19
SUBI    R19,0xFF
CPI     R19,0x64
BRCS    PC-0x03

This doesn't solve your question either, because lines of assembler code don't necessarily have the same execution time.

The only thing you can do is time the parts that you are interested in.

To answer the specific examples you listed:

Example 1

void setup() {
  Serial.begin(9600);
  t1 = micros();
  val = analogRead(pin);
  t2 = micros();
  Serial.print(t2-t1);
}

The speed of val = analogRead(pin) is determined by a few factors. The storing of the value to a register (val =) will take 1 instruction cycle. The reading of an analogue value (analogRead(pin)) from the PIN buffer will take a few instructions.

Example 2

void setup() {
  Serial.begin(9600);
  t1 = micros();
  int x = 1 + 2;
  t2 = micros();
  Serial.print(t2-t1);
}

The reason why int x = 1 + 2; takes 0 microseconds to execute is that the compiler optimises code that you write. Since the variable x is never used in its scope, the compiler just removes this line. Which means this line is not on the Arduino, and therefor is not executed. So what you're actually doing is this:

void setup() {
  Serial.begin(9600);
  t1 = micros();
  t2 = micros();
  Serial.print(t2-t1);
}

Even if it didn't remove the line, the addition of constatnts, i.e. 1 + 2 would be optimised to 3, the addition operation would never occur on the Arduino.

Upvotes: 5

Related Questions