I read that switch/case
is faster than if/else
, but after i searched a bit more i found this question (from 2012) saying that if/else
is faster (according to https://phpbench.com/) if use ===
instead of ==
.
I found this article saying that on PHP +7.2 switch/case
was optimized to use jump table
, but only if all the case statements are either integers or strings.
What about a code like this one bellow?
Which one is faster?
switch(true){
case($a === 'a'):
//
break;
case($b === 'b'):
//
break;
case($c === 'c'):
//
break;
}
vs
if($a === 'a'){
//
}elseif($b === 'b'){
//
}elseif($c === 'c'){
//
}
Would the first snippet use the jump table
? I rather use switch/case
instead of if/else
when there's more than 2 conditions, i felt that it's faster but i'm not sure.
Aucun commentaire:
Enregistrer un commentaire