mardi 19 avril 2016

Why nested if-statement is faster than using && operator?

Actually I'm wonder, why when I use multiple conditions into one if-statement is slower than multiple separated if-statement? In other word, why using && is slow?

Consider this:

$a = 1;
$b = 2;
$c = 3;
$d = 4;
$e = 5;

So the execution's time of this: 0.32209205627441

if ( $a == 1 && $b == 2 && $c == 3 && $d == 4 && $e == 5 ) {}

And this: 0.25026607513428

if ( $a == 1 ){
    if ( $b == 2 ) {
        if ( $c == 3 ) {
            if ( $d == 4 ) {
                if ( $e == 5 ) {}
            }
        }
    }
}

Online Benchmark

Aucun commentaire:

Enregistrer un commentaire