简体   繁体   中英

C++: Performance of Switch Statement vs Lookup tables

I tried to compare the performance of switch statement and lookup tables as below.

This is the code using switch statement

#include <stdio.h>

int main()
{
    int n = 3;

    for (long i = 0; i < 10000000; ++i) {  
        switch (n) {
        case 0:
            printf("Alpha");
            break;
        case 1:
            printf("Beta");
            break;
        case 2:
            printf("Gamma");
            break;
        case 3:
            printf("Delta");
            break;
        default:
            break;
        }
    }

    return 0;
}

And here is the code using lookup tables:

#include <stdio.h>

static char const * const Greek[4] = {
  "Alpha",
  "Beta",
  "Gamma",
  "Delta"
};

int main()
{
    int n = 3;

    for (long i = 0; i < 10000000; ++i) {  
        if (n >= 0 && n < 4) {
            printf(Greek[n]);
        }
    }

    return 0;
}

I run two program on ubuntu 14.04, gcc version 4.8.4, using perf version 4.4.13 to analyze performance. And result:

  • Switch Statement: 6.764077822 seconds
  • Lookup tables: 6.665140483 seconds

I don't know why Switch Statement run slower than Lookup tables. As I known, Switch Statement using jump table, i think it should run faster than Lookup tables in my program (it has additional if statement).

If you are compiling with optimizations, your code has no switch and no lookup table. I's just a for loop which calls printf() many times with the same fixed string. Any minimally reasonable compiler will in fact detect that n = 3 is never changed and optimize it out. Maybe you can add a n = rand() % 4; just inside the loop.

On the other end if you are not compiling with optimizations, taking timings is meaningless.

Coming to your question, I expect the lookup table to be faster than the switch statement, since the switch statement will totally thrash the Branch Prediction, while the if will be no problem, being always true.

Your benchmark is entirely inadequate to measure switch /lookup-table performance: Virtually all time is spend within the printf() calls. If there is any effect from the switch /lookup-table difference, it is dwarfed by the measurement-noise due to the large total runtime.


For reference: I expect a table lookup in a hot, tight loop to be only about ten clock-cycles. On a 2 GHz CPU, that's just 0.05 seconds for all 10000000 iterations (rough estimate, can easily be wrong by a factor of two, but that doesn't affect the overall assessment). That's on the order on 1% of your total runtime!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM