Vinze Posted May 16, 2007 Share Posted May 16, 2007 Hi, this article (page 2) says this: A case statement produces roughly 1/5th the amount of machine code as an if/else structure that completes the same task. Is this really true? So when I have the following: <?php $something = true; if($something) { // Do something } else { // Do something else } ?> Should I instead use: <?php $something = true; switch($something) { case true: // Do something break; default: // Do something else } ?> It seems to be less efficient to me, and it is less easy to read, but if it really gives such a speed increase then I might consider using it when working on a big project... Quote Link to comment https://forums.phpfreaks.com/topic/51710-switch-faster-than-if/ Share on other sites More sharing options...
kenrbnsn Posted May 16, 2007 Share Posted May 16, 2007 I find case statements much easier to ready, especially if there are many different options. There's probably not much time saving in a simple example like your's. Ken Quote Link to comment https://forums.phpfreaks.com/topic/51710-switch-faster-than-if/#findComment-254749 Share on other sites More sharing options...
cooldude832 Posted May 16, 2007 Share Posted May 16, 2007 I think switch is actually defined as a function of multiple if elseif commands so I would think switch would be slower, but you are talking about micro seconds just like the arguement with echo and print (I know that echo is void, but) Switches are considered better anyways though because of ease of reading. Quote Link to comment https://forums.phpfreaks.com/topic/51710-switch-faster-than-if/#findComment-254762 Share on other sites More sharing options...
cooldude832 Posted May 16, 2007 Share Posted May 16, 2007 However for a single test If is better because it does not require the default break for example if(!ISSET($VAR)){exit();} is better than turning it into a switch Quote Link to comment https://forums.phpfreaks.com/topic/51710-switch-faster-than-if/#findComment-254764 Share on other sites More sharing options...
Vinze Posted May 16, 2007 Author Share Posted May 16, 2007 I find case statements much easier to ready, especially if there are many different options. There's probably not much time saving in a simple example like your's. Ken Of course not in this little example, but I suppose if it really is 5 times faster then it would be able to give it just that extra push when it's a big application. If there are multiple options, of course switch is preferred, but if there's just if and else, would it really be faster? As cooldude832 said, I think switch is actually defined as a function of multiple if elseif commands so I would think switch would be slower Quote Link to comment https://forums.phpfreaks.com/topic/51710-switch-faster-than-if/#findComment-254774 Share on other sites More sharing options...
cooldude832 Posted May 16, 2007 Share Posted May 16, 2007 i'm not 100% sure about my statement of it being a func of if elseif else with default=else and everything else being if/elseif but it would make sense. Even still we are talking minimal differences I think even with 100 item switch because it executes the logic step of each, NOT the block which is generally what slows execution. Quote Link to comment https://forums.phpfreaks.com/topic/51710-switch-faster-than-if/#findComment-254783 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.