Reputation: 10350
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 ) {}
}
}
}
}
Upvotes: 0
Views: 1979
Reputation: 255005
One obvious difference is that the long chained expression performs an extra cast to bool after every comparison (since the &&
operator requires that):
11 11 > IS_EQUAL ~16 !0, 1
12 > JMPZ_EX ~16 ~16, ->15
13 > IS_EQUAL ~17 !1, 2
14 BOOL ~16 ~17
15 > > JMPZ_EX ~16 ~16, ->18
16 > IS_EQUAL ~18 !2, 3
17 BOOL ~16 ~18
18 > > JMPZ_EX ~16 ~16, ->21
19 > IS_EQUAL ~19 !3, 4
20 BOOL ~16 ~19
21 > > JMPZ_EX ~16 ~16, ->24
22 > IS_EQUAL ~20 !4, 5
23 BOOL ~16 ~20
24 > > JMPZ ~16, ->25
vs
17 42 > IS_EQUAL ~30 !0, 1
43 > JMPZ ~30, ->52
18 44 > IS_EQUAL ~31 !1, 2
45 > JMPZ ~31, ->52
19 46 > IS_EQUAL ~32 !2, 3
47 > JMPZ ~32, ->52
20 48 > IS_EQUAL ~33 !3, 4
49 > JMPZ ~33, ->52
21 50 > IS_EQUAL ~34 !4, 5
51 > JMPZ ~34, ->52
Apart of that the evaluation for both implementations looks pretty similar.
Upvotes: 2