Jump to content

Recommended Posts

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...

Link to comment
https://forums.phpfreaks.com/topic/51710-switch-faster-than-if/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/51710-switch-faster-than-if/#findComment-254762
Share on other sites

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
Link to comment
https://forums.phpfreaks.com/topic/51710-switch-faster-than-if/#findComment-254774
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/51710-switch-faster-than-if/#findComment-254783
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.