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.
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.
Prelude said it best. You cannot make a blanket general statement concerning which is faster..switch's or if's.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.
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.
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"