CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2002
    Location
    Germany
    Posts
    340

    Efficiency of switch statement.

    Hi all,

    Just a general question. I heard today that the switch statement in C++ is more efficient than using if-elseif structure. Someone told me that the order in which the 'case' statements appear is not important anymore. IS this true? Does C++ create some kind of a jump table for a switch statement or something?

    Thanks,

    Xargon

  2. #2
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    Implementations are compiler dependent, but yes compilers can often optimize switch statements better than if-else if-else blocks. In a worst case scenario a case statement should perform equally with if-else if-else blocks. However, since teh compilation of switch statements is compiler dependent (and can sometimes be reduced to if-else if-else blocks) then the order of cases can matter. If you are concerned about a particular example, profile it!

    Hope this helps!

    - Kevin

  3. #3
    Join Date
    Sep 2002
    Posts
    1,747
    We had a nice discussion that looked over this a little while back on these forums. It was discovered that the compilers we looked at often did create a jump table when the number of switch statements exceeded some lower bound. However, as the thread explores, there are faster methods available as well. And in comparison to if's, I think you will need to doyour own profiling and checking of the assembly to see, but my hypothesis in that thread was that the ifs would be taken care of at the same level of optimization as the switch if the arguments are the same, since I would think that a good compiler would perform this translation on the intermediate code phase and not during semantic translation, but I never checked it out...
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

  4. #4
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    As Kevin already said, the way a specific compiler translates (into machine code) a specific switch cannot be predicted in general. It is implementation dependent. However, understanding what optimizing possibilities a compiler has can help writing faster switch statements. One obvious thing is that if the case constants are (nearly) successive (case 1:, case 2:, case 3:...) -- as opposed to "random" (case 1:, case 123456:, case -123:, ...) -- the compiler has a better chance to build up a jump table. This does not mean that it has to produce better code, it means that it could produce better code.

    On the other hand, I have to stress out what Kevin already correctly stated: do not optimize if you haven't profiled.

    Also note that the decision to use a switch() or if/else if/.... can massively improve your code readability, debugging time and maintenance costs. The same is true for organizing switch() statements: it is rarely worth to build up a switch that is tuned for raw performance and thus is constructed in terms of the programming language instead of being contructed in terms of the problem to solve.

    Someone once said (I think it was Scott Meyers, not sure, though):
    The two rules of optimizing are:
    1. Don't do it.
    2. Don't do it yet.

    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured