ifとswitchどっちがはやいねん(文字列比較)

ということでベンチとった。

<?php
require_once 'Benchmark/Timer.php';

function useSwitch($value) {
    $a = 0;
    switch($value) {
    case 'foo':
        $a = 2;
        break;
    case 'bar':
        $a = 3;
        break;
    case 'baz':
        $a = 4;
        break;
    default:
        $a = 5;
    }
}

function useIf($value) {
    $a = 0;
    if($value == 'foo') {
        $a = 2;
    } elseif($value == 'bar') {
        $a = 3;
    } elseif($value == 'baz') {
        $a = 4;
    } else {
        $a = 5;
    }}
function useIfType($value) {
    $a = 0;
    if($value === 'foo') {
        $a = 2;
    } elseif($value === 'bar') {
        $a = 3;
    } elseif($value === 'baz') {
        $a = 4;
    } else {
        $a = 5;
    }
}
$target_functions = array('useSwitch', 'useIf', 'useIfType');
$target_arguments = array('foo', 'bar', 'baz', 'something');

$timer = new Benchmark_Timer();

$timer->start();
foreach($target_functions AS $func) {
    foreach($target_arguments AS $arg) {
        //$timer->setMarker("$func('$arg') before");                                                                 
        for($i= 0; $i<10000; $i++) $func($arg);                                                                      
        $timer->setMarker("$func('$arg')");
    }
}

$timer->stop(); 
$timer->display();

$aに数値代入しているのは何となく、なんか処理入れといたほうがいいかなーと思ったから。
自宅サーバのPHP5.2の結果。

---------------------------------------------------------------------
marker                   time index            ex time         perct   
---------------------------------------------------------------------
Start                    1221013617.78282700   -                0.00%
---------------------------------------------------------------------
useSwitch('foo')         1221013617.84077700   0.057950         8.26%
---------------------------------------------------------------------
useSwitch('bar')         1221013617.90401800   0.063241         9.02%
---------------------------------------------------------------------
useSwitch('baz')         1221013617.97475400   0.070736        10.09%
---------------------------------------------------------------------
useSwitch('something')   1221013618.04148700   0.066733         9.52%
---------------------------------------------------------------------
useIf('foo')             1221013618.09349300   0.052006         7.42%
---------------------------------------------------------------------
useIf('bar')             1221013618.15141800   0.057925         8.26%
---------------------------------------------------------------------
useIf('baz')             1221013618.21601200   0.064594         9.21%
---------------------------------------------------------------------
useIf('something')       1221013618.27801400   0.062002         8.84%
---------------------------------------------------------------------
useIfType('foo')         1221013618.32627600   0.048262         6.88%
---------------------------------------------------------------------
useIfType('bar')         1221013618.37754300   0.051267         7.31%
---------------------------------------------------------------------
useIfType('baz')         1221013618.43264300   0.055100         7.86%
---------------------------------------------------------------------
useIfType('something')   1221013618.48394000   0.051297         7.32%
---------------------------------------------------------------------
Stop                     1221013618.48402100   0.000081         0.01%
---------------------------------------------------------------------
total                    -                     0.701194       100.00%
---------------------------------------------------------------------

ifのが早いのね。そして、どうせ文字列比較するなら===使っとけということですね。