doutang1856 2010-08-03 18:48
浏览 129
已采纳

“if”与“switch”[重复]

Possible Duplicate:
is “else if” faster than “switch() case” ?

I've encountered a lot of situations lately where I have very simple conditionals and need to branch application flow. The "simplest" means of accomplishing what I'm doing is just a plain old if/elseif statement:

if($value == "foo") {
    // ...
} elseif($value == "bar") {
    // ...
} elseif($value == "asdf" || $value == "qwerty") {
    // ...
}

...but I'm also considering something like:

switch($value) {
    case "foo":
        // ...
        break;
    case "bar":
        // ...
        break;
    case "qwer":
    case "asdf":
        // ...
}

This seems a little less readable, but perhaps it's more performant? However, when there are more and more "or" expressions in the conditional, it seems that the switch statement is much more readable and useful:

switch($value) {
    case "foo":
        // ...
        break;
    case "bar":
    case "baz":
    case "sup":
        // ...
        break;
    case "abc":
    case "def":
    case "ghi":
        // ...
        break;
    case "qwer":
    case "asdf":
        // ...
}

I've also seen options where code flow is branched using arrays and functions:

function branch_xyz() {/* ... */}
function branch_abc() {/* ... */}
function branch_def() {/* ... */}

$branches = array(
    "xyz"=>"branch_xyz",
    "abc"=>"branch_abc",
    "def"=>"branch_def"
);
if(isset($branches[$value])) {
    $fname = $branches[$value];
    $fname();
}

This last option also presumably has the benefit of being distributable across multiple files, though it is pretty ugly.

Which do you feel has the most advantages with the fewest tradeoffs in terms of performance, readability, and ease of use?

  • 写回答

4条回答 默认 最新

  • dougu2006 2010-08-03 18:54
    关注

    Personally, I find the switch a lot more readable. Here's the reason:

    if ($foo == 'bar') {
    } elseif ($foo == 'baz') {
    } elseif ($foo == 'buz') {
    } elseif ($fou == 'haz') {
    }
    

    Condensed like that, you can easily see the trip up (be it a typo, or a honest difference). But with a switch, you know implicitly what was meant:

    switch ($foo) {
        case 'bar': 
            break;
        case 'baz': 
            break;
        case 'buz': 
            break;
        case 'haz': 
            break;
    }
    

    Plus, which is easier to read:

    if ($foo == 'bar' || $foo == 'baz' || $foo == 'bat' || $foo == 'buz') {
    }
    

    or

    case 'bar':
    case 'baz':
    case 'bat':
    case 'buz':
        break;
    

    From a performance standpoint... Well, don't worry about the performance. Unless you're doing a few thousand of them inside of a tight loop, you won't even be able to tell the difference (the difference will likely be in the micro-second range, if not lower).

    Go for the method that you find the most readable. That's the important part. Don't try to micro-optimize. Remember, Premature Optimization Is The Root Of All Evil...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于#flink#的问题:关于docker部署flink集成hadoop的yarn,请教个问题flink启动yarn-session.sh连不上hadoop
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题