Thread: if is faster than switch?

  1. #31
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Bubba, no offence mate, but what are you talking about? Have you actually read this thread?

    >>using ifs or switches is not going to gain you that many cycles in your code.<<

    Well, I agree with that, even if nothing else in your post.

  2. #32
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    My response to statements like this is: "Prove it." The language doesn't define speed for such things, so it's up to the implementation. The constrains on a switch are such that even a bad implementation can (in theory) create fast machine code. An if has leeway for doing more work and thus creating slower machine code than a switch in said bad implementation. So (in theory), on a stupid compiler a switch can be faster through luck of definition. On good optimizing compilers, one or the other will probably be faster, but the difference will be vanishingly small. So write the code that's clearest and don't worry about speed issues until you know for a fact that you have a problem.
    Prelude said it best. You cannot make a blanket general statement concerning which is faster..switch's or if's.

    And what is there to agree with in my post. I simply said that this ludicrous claim is not true in all cases, regardless of what some book said.

    It depends heavily on how the compiler actually codes the switch and/or if in assembly and that depends on who created the compiler. So this is too compiler dependent to and too programmer dependent as well to claim it is true in all cases.

    Your disassemblies prove nothing other than how MSVC chose to compile the given code. MSVC is one of a hundred C compilers and I'm sure the Intel, DJGPP, Dev-C,Watcom C, Borland, compiler(s) do not do it the same way. Show me in writing where it tells compiler writers they must compile a switch or if in a certain way according to a certain standard and I'd say yes your statements are true. Unfortunately there is no standard. Go look at the standards required for a language to be considered ANSI C.
    Last edited by VirtualAce; 03-06-2004 at 06:06 AM.

  3. #33
    Registered User
    Join Date
    May 2002
    Posts
    15

    I dissagree

    I recently dropped out of my data structures class because of too many classes. However, I did get the oppertunity to go over a little bit of what is called big-oh anaylisys. Essentially it's a way to detrmini if one data structure should be used over another. Believe me, there are examples just like this where a small value won't make a difference, but a large one will. If the 'if' statements are better to use when the switch has a value of a hundred ormore that's probably because an 'if' statement is linear and a switch statement is probably quadratic. Maybe someone that's taken a data structure(algorithms) class can confirm this.
    "one nation under god"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM
  4. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM