Instruction execution times...

So, I'd like to understand how long different instructions take to execute....does anyone know of a reference to allow a shaved monkey to work it out?

For example, I'd like to see how long it takes to run the following;

If (X>3) {.....

compared to

Switch (x){
Case 4: {......

Write the code, run it, and time it, using micros()..

Regards,
Ray L.

The answer you get will depend on the data type. I suspect that there will be no significant difference between the 'if' version and the 'switch' version. The advantage of the 'if' is that you can use relational comparison (x>3) where with switch you can only test for equality (x==4).

scrumfled:
So, I’d like to understand how long different instructions take to execute…does anyone know of a reference to allow a shaved monkey to work it out?

For example, I’d like to see how long it takes to run the following;

If (X>3) {…

compared to

Switch (x){
Case 4: {…

You can write your own program analysis routines, but you have to be very careful that you are actually measuring what you want.

This can be complicated, because Arduino uses a very smart “optimizing” compiler, that sorts out every kind of code that is not needed.

For example: You do calculations only from constants.
Result: Arduino will do the calculations at compile time and insert the constants into the program code. Even if you think you are doing actually complicated calculations, Arduino just might put a single numeric constant into the code.

Other example: You do calculations from input, but you do not use the result. For example you do not send the result to Serial or put it on a display.
Result: Arduino will recognize that you actually do not use the calculated result, and so Arduino will optimize the calculation away, it simply is not calculated.

So for timing analyses always be sure:

  • take actual input for your calculations
  • do actual output with your calculations result
    Then you will see fair timing values.

Here is an example program:

/ Timing test for Arduino without anything connected to the board
byte pins[]={2,3,4,5};
#define NUMPINS (sizeof(pins))

int countDigitalHigh()
{
  int result=0;
  for (int i=0;i<NUMPINS;i++) result+=digitalRead(pins[i]);
  return result;
}

int func1()
{
  int x=countDigitalHigh();
  if (x>3) return 1;
  else return 0;
}

int func2()
{
  int x=countDigitalHigh();
  switch(x)
  {
    case 4: 
      return 1;
      break;
    default:
      return 0;
  }
}


void setup() {
  Serial.begin(9600);
  Serial.println();
  for (int i=0;i<NUMPINS;i++) pinMode(pins[i],INPUT_PULLUP);
}

unsigned long time1, time2;
long result1, result2;
void loop() {
  time1=micros();
  for (int i=0;i<10000;i++) result1+=func1();
  time1=micros()-time1;
  
  time2=micros();
  for (int i=0;i<10000;i++) result2+=func2();
  time2=micros()-time2;
  
  Serial.print("func1():\t");  
  Serial.print(result1);
  Serial.print("\tmilliseconds: ");
  Serial.println(time1/1000.0,3);

  Serial.print("func2():\t");  
  Serial.print(result2);
  Serial.print("\tmilliseconds: ");
  Serial.println(time2/1000.0,3);
  
  Serial.println();
  delay(3000);
}

The input is taken by digitalRead from 4 input pins which are pulled HIGH.
The output is sent to Serial, but the Serial print is done after the timing is calculated, so sending on Serial will have no influence on the timing.

And: The timing is only true for the actual code.

For example if you write some “switch case” code but there are only two possible results, perhaps Arduino creates optimized code that might be the same (or even a better optimized) as a simple if-Statement.