switch case vs elseif

Hi
i tried to search on the speed issue of this 2 versions
as im time critical on led base
what is the faster option on 5cases and default can i check this somwhere
somehow

Thanks

It seems the switch is a little faster than the if.
I tried using a loop:

void setup() {
  Serial.begin(115200);
  while (!Serial);
  unsigned int elapsed;
  int x = 0;
  int y = 0;
  int n = 10000;
  unsigned int timer;

  timer = micros();
  for (int i = 0; i < n; i++) {
    // test the case
    x = random(5);
    switch (x) {
      case 0: y = y + 1;
      case 1: y = y + 5;
      case 2: y = y + 2;
      case 3: y = y + 7;
      case 4: y = y + 3;
      default: y = 0;
    }
  }
  elapsed = micros() - timer;
  Serial.print("Case : ");
  Serial.println(elapsed);

  timer = micros();
  for (int i = 0; i < n; i++) {
    x = random(5);
    // test the if
    if (x == 0) y = y + 1;
    else if (x == 1) y = y + 5;
    else if (x == 2) y = y + 2;
    else if (x == 3) y = y + 7;
    else if (x == 4) y = y + 3;
    else y = 3;
  }
  elapsed = micros() - timer;
  Serial.print("If : ");
  Serial.println(elapsed);
}

void loop() {
}

10000 iterations give 49308 microseconds for the switch and 49328 microseconds for the if (on a leonardo).

10,000 iterations, and you believe a 20 uSec difference is meaningful? That is random noise. A single interrupt can take more time than that. That is truly "picking the fly poop out of the pepper".

There will, in the overwhelming majority of cases, be virtually no difference between if's and switch, unless there are many, many ifs.

Regards,
Ray L.

how fast do you need to be?

you could use an array to map the value

y += val [x];

The 'y=y+...' was only some instructions I threw in the blocks to make them alive. It may not be relevant to the OP's question.

And I agree that 20 us is a very short difference, but it remains the constant whenever I run this test case. So the conclusion is that both cases are equivalent.

Your 'switch' statement is missing the "break;" after each case so the lower numbered cases are executing all of the higher numbered cases. Even worse, they all drop through to the default case: y=0; so the entire switch statement can be optimized to "y=0;".

I use switch where possible, simply for more compact, readable code.
Both have their place for a variety of reasons.

And: time value should be 'unsigned long'
And: The value in 'y' is never used so the entire loop can be optimized away (make 'y' volatile to avoid that optimization.

The result is that 'switch' is 120 nanoseconds per loop faster.

void setup() {
  Serial.begin(115200);
  while (!Serial);

  int x = 0;
  volatile int y = 0;
  int n = 10000;
  unsigned long timer;

  timer = micros();
  for (int i = 0; i < n; i++) {
    // test the case
    x = random(5);
    switch (x) {
      case 0: y = y + 1; break;
      case 1: y = y + 5; break;
      case 2: y = y + 2; break;
      case 3: y = y + 7; break;
      case 4: y = y + 3; break;
      default: y = 0;
    }
  }
  unsigned long elapsed1 = micros() - timer;
  Serial.print("Case: ");
  Serial.println(elapsed1);

  y = 0;
  timer = micros();
  for (int i = 0; i < n; i++) {
    x = random(5);
    // test the if
    if (x == 0) y = y + 1;
    else if (x == 1) y = y + 5;
    else if (x == 2) y = y + 2;
    else if (x == 3) y = y + 7;
    else if (x == 4) y = y + 3;
    else y = 0;
  }
  unsigned long elapsed2 = micros() - timer;
  Serial.print("If: ");
  Serial.println(elapsed2);

  Serial.print("Difference (microseconds): ");
  Serial.println((float)(elapsed2 - elapsed1) / n);

}

void loop() {
}

'switch' is a shortcut for the very common case where an integer value is being compared agains a fixed list of integer constants. If that is your case, use 'switch' to make the code more readable. Trust the compiler to optimize the code so that there is no significant difference in speed.

Oups, you're right, I did make many mistakes in that example code... Thanks for the update !