Do compilers optimize switches differently than long if-then-else chains?
问题 Suppose I have N different integral values known at compile time, V_1 through V_N. Consider the following structures: const int x = foo(); switch(x) { case V_1: { /* commands for V_1 which don't change x */ } break; case V_2: { /* commands for V_1 which don't change x */ } break; /* ... */ case V_N: { /* commands for V_1 which don't change x */ } break; } versus const int x = foo(); if (x == V_1) { /* commands for V_1 which don't change x */ } else if (x == V_2) { /* commands for V_2 which