IF x SWITCH - Simulating

-3

I think we all always have to do some dealings in conditions with few values.

I made a test between IF and SWITCH .

  

All content was created for SIMULAR a more identical structure   possible between IF and SWITCH .

Class file: ifXswitch.php

<?php

class Teste {


    public function fc_if($v) {

        // Marcador de tempo 1
        $t1 = microtime(true);


        // Script
        if ($v == 1) { echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula(1); }
        elseif ($v == 2) { echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula(2); }
        elseif ($v == 3) { echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula(3); }
        elseif ($v == 4) { echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula(4); }
        elseif ($v == 5) { echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula(5); }
        elseif ($v == 6) { echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula(6); }
        elseif ($v == 7) { echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula(7); }
        elseif ($v == 8) { echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula(8); }
        elseif ($v == 9) { echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula(9); }
        else { echo 'Número inválido'; }


        // Marcador de tempo 2
        $t2 = microtime(true);


        // Tempo final
        $t3 = $t2 - $t1;


        return $t3;
    }

    public function fc_switch($v) {

        // Marcador de tempo 1
        $t1 = microtime(true);


        // Script
        switch ($v) {

            case '1':
            echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula(1);
            break;

            case '2':
            echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula(2);
            break;

            case '3':
            echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula(3);
            break;

            case '4':
            echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula(4);
            break;

            case '5':
            echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula(5);
            break;

            case '6':
            echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula(6);
            break;

            case '7':
            echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula(7);
            break;

            case '8':
            echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula(8);
            break;

            case '9':
            echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula(9);
            break;

            default:
            echo 'Número inválido';
            break;
        }


        // Marcador de tempo 2
        $t2 = microtime(true);


        // Tempo final
        $t3 = $t2 - $t1;


        return $t3;
    }


    public function calcula($v) {

        $vf = pow($v,5);

        sleep(1);

        return $vf;
    }


    public function resultado($t_if, $t_switch) {

        echo '<hr>';
        echo '<br> Tempo IF: ' . $t_if;
        echo '<br> Tempo SWITCH: ' . $t_switch;

        if ($t_if < $t_switch) $r = 'IF';
        elseif ($t_if == $t_switch) $r = 'Empate';
        elseif ($t_if > $t_switch) $r = 'SWITCH';
        else $r = 'Erro';

        echo '<br> Menor tempo: ' . $r;
    }

}

?>

Executor: mitrotime.php

<?php

include_once 'ifXswitch.php';

$exemplo = new Teste();
$t_if = $exemplo -> fc_if(9);
$t_switch = $exemplo -> fc_switch(9);
$exemplo -> resultado($t_if, $t_switch);

?>

In the test run, it varies a lot, time wins the IF time the SWITCH, and even unbelievably gave a tie once.

I would like to know, among them, if one has an advantage over the other in some aspect (eg with SWITCH numbers would be better!?), or is it about necessity and organization?

    
asked by anonymous 28.03.2018 / 14:59

1 answer

3

If you run more than a billion loops, you may feel some noticeable difference to the user, but in general they do not make any difference that has meaning, 15 conditions for example would not even notice anything.

Keep in mind that each version of PHP has a different performance, version 7 inclusive is much more efficient than 5, there is also the possibility of using "cache" (it does not have anything to do with browser cache), it is similar to JIT (maybe it can be called that), read more at:

To summarize, there is no significant difference between switch and if , what they differ is in your needs, for example the way you used if and switch you wrote the code already inefficiently in both cases, ie a good part of performance losses is the way we use and not between types.

The real performance problems are in the way we write and not in two language features, of course there are exceptions, but almost all are micro-optimization only, which is something people should stop worrying and worry more about the serious mistakes that programmers make that will actually affect speed.

Someone here may come and say that switch is better, but this in PHP is not necessarily a reality, since the way you write your IFs can result in something more performative (micro-optimization, which usually it is usually insignificant), and as I said there are variations of behavior between versions of PHP, but it is no use going too deep.

What you need to understand is that IF has a better time, place, and way of working, and the same goes for SWITCH , has a better time, place, and way of working.

What will slow down both is the way you write them, if you know how to deal well with the conditions is able to both have the same result (as I said use as needed).

Other factors that can consume more of the script, such as repeated function executions at each check, which is a fairly common error among programmers, for example:

function foo()
{
     return file_get_contents('./version');
}

if (foo() == 1) {
    echo 'Versão 1 do App, atualize para a versão 2 ou 3, este esta obsoleto';
} elseif (foo() == 2) {
    echo 'Versão 2 do App';
} elseif (foo() == 3) {
    echo 'Versão 3 do App';
}

Note that in the above code, it was executed at least 3 times (depending on the contents of the ./version file), now you can do this:

$versao = foo();

if ($versao == 1) {
    echo 'Versão 1 do App, atualize para a versão 2 ou 3, este esta obsoleto';
} elseif ($versao == 2) {
    echo 'Versão 2 do App';
} elseif ($versao == 3) {
    echo 'Versão 3 do App';
}

You're sure to only run foo() , and since the answer is already saved in $versao , this will make the script more performative.

Now a suggestion about your code, the problem I see in your code is unnecessary repetition, for example this:

if ($v == 1) { echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula(1); }
elseif ($v == 2) { echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula(2); }
elseif ($v == 3) { echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula(3); }
elseif ($v == 4) { echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula(4); }
elseif ($v == 5) { echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula(5); }
elseif ($v == 6) { echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula(6); }
elseif ($v == 7) { echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula(7); }
elseif ($v == 8) { echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula(8); }
elseif ($v == 9) { echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula(9); }
else { echo 'Número inválido'; }

It could simply be this:

if ($v >= 1 && $v <= 9) {
    echo '<br>' . $v . ' elevado a 9 = ' . $this -> calcula($v);
} else {
    echo 'Número inválido';
}

Now, you have reduced your code to two lines, which will certainly improve the performance of the parser (which is part of the script interpreter), which I quoted at the beginning of the answer, the problem is more on how we write IFs than on other factors.

    
28.03.2018 / 15:10