Who is faster in PHP switch and ifelse?

Time:2022-1-9

In the case of multiple if conditional judgments, it will be clearer for the code to use switch instead of ifelse. What about their efficiency comparison? It is found from the PHP manual that someone has compared it, and he has also experimented with his code:

$s = time();
for ($i = 0; $i < 1000000000; ++$i) {
    $x = $i % 10;
    if ($x == 1) {
        $y = $x * 1;
    } elseif ($x == 2) {
        $y = $x * 2;
    } elseif ($x == 3) {
        $y = $x * 3;
    } elseif ($x == 4) {
        $y = $x * 4;
    } elseif ($x == 5) {
        $y = $x * 5;
    } elseif ($x == 6) {
        $y = $x * 6;
    } elseif ($x == 7) {
        $y = $x * 7;
    } elseif ($x == 8) {
        $y = $x * 8;
    } elseif ($x == 9) {
        $y = $x * 9;
    } else {
        $y = $x * 10;
    }
}
print("if: " . (time() - $s) . "sec\n");

$s = time();
for ($i = 0; $i < 1000000000; ++$i) {
    $x = $i % 10;
    switch ($x) {
        case 1:
            $y = $x * 1;
            break;
        case 2:
            $y = $x * 2;
            break;
        case 3:
            $y = $x * 3;
            break;
        case 4:
            $y = $x * 4;
            break;
        case 5:
            $y = $x * 5;
            break;
        case 6:
            $y = $x * 6;
            break;
        case 7:
            $y = $x * 7;
            break;
        case 8:
            $y = $x * 8;
            break;
        case 9:
            $y = $x * 9;
            break;
        default:
            $y = $x * 10;
    }
}
print("switch: " . (time() - $s) . "sec\n");

After 1000000000 cycles and adding arithmetic operations to each judgment condition, we found that switch is more efficient and runs faster. The result on my computer is:

// if: 301sec
// switch: 255sec

Although switch is more efficient, there are also some points to note. First, the judgment value can only be a number, floating point number or string. Secondly, each judgment is an ordinary = = judgment, that is, the following judgment results are not necessarily similar to yours:

$string = "2string";

switch ($string) {
    case 1:
        echo "this is 1";
        break;
    case 2:
        echo "this is 2";
        break;
    case '2string':
        echo "this is a string";
        break;
}

// this is 2

Yes, it is still the type strong conversion problem in = = comparison. When comparing string and int values, it is forced to int type, and the result of “2string” strong conversion is exactly 2. Therefore, when using switch, you should ensure that the comparison value is consistent with the type of each case, otherwise unpredictable errors may occur.

Reference code:https://github.com/zhangyue0503/dev-blog/blob/master/php/201911/source/PHP%E7%9A%84switch%E5%92%8Cifelse%E8%B0%81%E6%9B%B4%E5%BF%AB%EF%BC%9F.php

Reference Manual:https://www.php.net/manual/zh/control-structures.switch.php

Official account: hard core project manager

Add wechat / QQ friends: [xiaoyuezigonggong / 149844827] get free PHP and project management learning materials

Tiktok, official account, voice, headline search, hard core project manager.

Station B ID: 482780532