Here's a *if vs Switch* Benchmark program

This is a discussion on Here's a *if vs Switch* Benchmark program within the C++ Programming forums, part of the General Programming Boards category; Just thought you guys and gals would like to try a if vs Switch bench-mark code. It might not be ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    90

    Here's a *if vs Switch* Benchmark program

    Just thought you guys and gals would like to try a if vs Switch bench-mark code. It might not be the best but it seem to makes a decent point. On my machine Dell 3.00 Ghz -- 2 gigs of RAM "switch" was only one ms faster than "if" and when I ran it a second time, it was reverse so they are basically equal. It's only a matter of taste.

    I found it here:
    C++ Speed Test: If vs Switch Statement | Programming Designs
    ...

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    Only five cases and very simple branching though. I wonder if it was a little more complex would there be more significant differences?
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Talk about experimental bias!

    How about reversing the order of if/else if tests, so you test the constant expected value (5) FIRST?

    Or how about say case 1000: case 2000: case 3000 etc, rather than consecutive integers.

    Or how about results which are NOT integer results known at compile time?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    This comparison does not show anything. Despite of the fact, that you literally didn't use any optimizations (I saw -O2 only), you used std::string to store result, which is a very costy operation (comparing to branches).

    Your code also jumps in 99,99% of tests into the "default" case and the last if-else statement. If you hadn't use std::string the switch case would have got much better results. All cases should be used equally.

    I haven't checked this code in the debugger, but I'm sure this switch case was generated with O(1) access using regular switch table. I'm not sure whether GCC considered this if-else as switch, but even if it did, it don't think it would be faster than the explicit switch.
    Last edited by kmdv; 02-28-2011 at 08:09 AM.
    I never put signature, but I decided to make an exception.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    I actually found this interesting since it gave me a reason to look at some assembly code. It can be helpful to get familiar with what compilers are generating.

    If the code is compiled as presented with optimizations, the compiler will see that val is constant and remove the if/switch statements, as expected. It didn't remove the loop, I guess since the compiler cannot know what side effects occur in the std::string assignment operator.

    I changed it so val is assigned an random number modulus 7 to prevent this.
    With the if statement, a cmp instruction is executed for each case. With the switch statement a single cmp instruction was executed to check for values over 5, otherwise it just used val as an index for an offset to select the string. My initial guess would have been that nearly identical code would be generated, so i learned something from this.

    So, interesting journey into compiler code generation and optimization. It does look like the switch code is more optimal in this case by some small amount (not that I'd ever consider performance when selecting between using if/switch statements).

    I used Visual Studio 2008 with full optimizations.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    Code:
    Your code also jumps in 99,99% of tests into the "default" case and the last if-else statement.
    Who me
    Code:
     If you hadn't use std::string the switch case would have got much better results. All cases should be used equally.
    Lets not get personal


    I know you're not talking about me. If I had wrote the code you best believe it be pack with in-line FASM some how. I just made it to charter-13 of Deitel C++. Now I can't wait to get out of school so I can explore C++. Just because the compile will do it all for me don't mean I don't want to peep under the s hood or do some things for myself. I don't have a true clue but interesting to figure that C++ can check SWITCH at bottom FIRST, but can't do the same for if ... if, if if, else, if, else etc. Kind of make since. It got to take it from the top to learn, but why loop EVERY statement, completely???

    All though I saw no difference but the better programmers like you guy did, as a newbee this tells me to use switch when ever possible once I get started and let the standard continue to bicker about it. Optimization is optimization even if it save you an single clock and optimization turn on don't mean it's totally optimized. Humans can be smarter, even by accident. I can't wait to learn C++.

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    as a newbee this tells me to use switch when ever possible once I get started and let the standard continue to bicker about it. Optimization is optimization even if it save you an single clock and optimization turn on don't mean it's totally optimized.
    Are you going to shoehorn all branching into a switch case in your programs now? You could, you know. But you will have to write additional functions to work out some Boolean logic in a switch case friendly way. The overhead from calling said functions will just spend any time that you save.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,555
    sharris, prefer using quote tags for quoting people and not code tags.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    Are you going to shoehorn all branching into a switch case in your programs now?
    Pay me no attention. I just been cocky because I'm writing programs beyond class assignments. I an expert at trial and error but can't explain what make it tick for days to weeks afterward so I might as well stay in the books. I use to always benchmark ASM code so I plan to find and use a lot of tools for bench-marking C++ code and to know what the compiler is doing. It be a while before I have any comments that makes any real since to others.


    Elysia, I keep forgetting about , that will not happen again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21