An optimized website is not only about fewer tags, styles or plugins, I also believe that resources on the server side need to be optimized for better performance.
So the use of a if
can help to execute several data that can be executed in one or by elseif
executing another different script.
if($var === 'a' || $var === 'b' || $var === 'c'){
//Ejecutar
}elseif($var1 === 'd'){
//Ejecutar
} else {
//Default
}
And in the use of a switch
it is executed in a very elegant way although it has its drawbacks, for example having to add a new one switch
when the variable is different, for example:
switch ($var) {
case 'a':
# code...
break;
case 'b':
# code...
break;
case 'c':
# code...
break;
default:
# code...
break;
}
switch ($var1) {
case 'd':
# code...
break;
}
Both uses seem simple, but which one occupies more resources, rather than an opinion, is an important issue that I think should be taken into account for applications with much more performance.
Foreword
Each use case could return different results for different reasons, so the reasoned statement in this answer is only valid in particular to this use case, although it could be defined as a fairly general case.
So we can say that, in this particular case, the performance of a
switch
/case
is superior to that of blocksif
/else
in terms of execution speed, but that in terms of memory usage aswitch
/ is worsecase
thanif
/else
.Also, in all cases the
switch
/ blockscase
would fail to compare the initial value of$letra
in the tests, that is0
, when comparing it to the lettera
, since it0 == 'a'
returnstrue
(the type of comparison usingswitch
/case
), while it0 === 'a'
returnsfalse
( type of comparison used in theif
):Test Environment
For testing I have created two files:
if.php
switch.php
Execution time
And I have executed consecutively and in the same PHP environment both scripts:
memory usage
To calculate the impact on memory I have used:
Base case:
In order to calculate the increase in time and memory of the
case
respect of theif
, a base case must be taken in which everything that is not related to the tests is taken into account:base.php
The results:
Results:
switch
.3,687
>1,109
(one232%
more).if
.576
<880
(one53%
more).Other use cases
As I said in the prologue to this answer, depending on the case, the results can increase or decrease.
A very quick example showing how a simple change can affect is by modifying this line in both scripts:
Now throwing more adjusted results between both:
switch
.2,938
>1,595
(one84%
more).Change the string to the first case of
or
delif
:It would bring the results even closer:
switch
.1,714
>1,287
(one33%
more).In the case you raise, you are mixing two different logics and therefore assuming a "against" that is not as such.
THE IF:
Note that the structure
if
that you propose, would execute the first part if the case of any of the ORs occurs.if it is not fulfilled, you analyze another different variable and if it is fulfilled you execute another different code,
and if nothing is fulfilled, it does the
else
.in summary:
logic 1: only if it is fulfilled
$var === 'a' || $var === 'b' || $var === 'c'
logic 2: only if it is true that
$var1 === 'd'
logic 3: only if the above are NOT met.
THE SWITCH:
On the contrary, the
switch
one you propose executes different code for each of the options (it is not the same). And then you make another completely different switch to check another variable. The following case may occur:$var=='a'. $var1 == 'd'
He
if
would only display the code that is in the first condition.would
switch
show them the code of the first conditiony
of the second condition.if they are not the same logics, these performances cannot be compared.
The switch is faster, it consumes less memory, since an if elseif has to check all the comparisons and the switch directly accesses the one it is and the others it does not check, here you have a more technical and detailed explanation, but basically it is that.
https://elbauldelprogramador.com/por-que-a-switch-es-faster-que-its-homologo-if-then-else/