Public Group

# A case of a switch. [C++]

This topic is 3705 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

void doStuff(int me)
{
switch (me)
{
case 0: // rearely happens, but when it does, it MUST return
return;
case 1:
//code
break;
case 2: // Happens nearly 100% of the time.
//code
break;
}
//code
}


Now, my question deals with this code snip. Would this function run faster if we moved the case 2 up a level? Would it run faster if we move case 0 down two levels? Is there any benefits to re-arranging the cases in a switch under M\$ Visual C++ or GCC? I seem to remember that there was a keyword like "rarely happens" or "frequently" that can be passed to compiler to expect a certain value. Can someone please remind me what it is? I seem to seen it somewhere and totally forgot. (I am just looking at my game loop in wndProc)

##### Share on other sites
Profile.

Although, if this is the game loop, it's quite probable that it only gets run a hundred times per second. A hundred nanoseconds per second isn't really worth the effort, you know.

##### Share on other sites
I can't give you feed back about weather moving the case would do anything for you. I know in the background the compiler is essentially making a look up table which probably uses a binary search to find the case. So, it might be likely that it doesn't matter what order you put them in. But, I am not expert at these things.

Just one suggestion, though. If you know for a fact that it will be run case 2 almost 100% of the time, you can instead you an if-else statement and make case 2 be the first one checked. If you don't know this is the case, you will need to profile.

Like ToohrVyk was saying, if this code is only run once per frame, it wouldn't have any effect to optimize it.

##### Share on other sites
Quote:
 Original post by ShadowPhoenixI seem to remember that there was a keyword like "rarely happens" or "frequently" that can be passed to compiler to expect a certain value. Can someone please remind me what it is? I seem to seen it somewhere and totally forgot.

Depends on compiler. The only way to know for sure (for each and every individual switch statement) is to examine the generated assembly.

This simply means that some statements will be completely inlined, some may be converted to if/else, some may use table lookup, some may use some other form of evaluation.

But there is no hard or reliable rule.

##### Share on other sites
what about just profiling it by yourself,
that way you can be sure about it.

like

time1 = getTicketCount();

for(int i=0;i<10000;i++)

{

}
time2 = getTicketCount();

//elapsed time = time2 - time1

Then you have time,
change the order of your switch and do it again

is it smaller or bigger or equal, (and the amount shows you if the afford is worth it.

##### Share on other sites
I doubt that any decent compiler will produce a LUT for only three cases. In any case, why would the minimal overhead of the switch statement be a concern? Even if you were to manually optimize in assembly, would you really expect to see any significant improvement in overall performance? You should really run a profiler to find bottlenecks, then do what ever algorithmic optimizations you can, and only after that consider low-level optimization, if you really have to.

##### Share on other sites
1. Yes, it will be faster when the most often taken branches are at the beginning of the switch.

2. As others said, in the WndProc, it won't matter. Profile, then optimize. (And before that: Make it run correctly and write clean, simple code, it is easier to optimize later... for you and the compiler).

3. There are such things as branch hint opcodes for the newer Intel processors, but only for SSE, IIRC. Again, such optimization is only worth the effort when you're writing highly performance-critical code.

4. Some compilers (MSVC, Intel C++) support Profile Guided Optimization, this is probably the best way to achieve optimally branching code.

##### Share on other sites
In most cases switch statements are converted into jump tables by the compiler. So the 12th statement of the switch statement is ran just as fast at the first. So it should not matter in general what the order is. Now what I really just got done staying is not really true. A lot depends on the settings you have for your project and the way the complier optimizes with those settings. If you have a very short switch block it might turn it into an if-then-else block.

So in this case it might matter but with a larger switch block it won't.

theTroll

##### Share on other sites
Thank you for your help everyone! I am just working on a function that gets called 130+ times a second. (100 repaints + other stuff) as my main loop.
Does anyone know anything about __builtin_expect ?

##### Share on other sites
Quote:
 Original post by ShadowPhoenixThank you for your help everyone! I am just working on a function that gets called 130+ times a second. (100 repaints + other stuff) as my main loop.
Not enough to be worth optimising.

• 38
• 15
• 9
• 23
• 10