I\'m iterating through an array and sorting it by values into days of the week.
In order to do it I\'m using many if
statements. Does it make any differ
In general, "else if" style can be faster because in the series of ifs, every condition is checked one after the other; in an "else if" chain, once one condition is matched, the rest are bypassed.
The fastest would be a table dispatch, which is what a switch statement gets optimized into when there are enough cases in it (if there are few cases in a switch, it gets translated into a series of if-else checks in the resulting machine code).
I would put another vote in for opting for a switch() statement instead.
You can have a look at phpbench
But to be honest if you want to optimize at this level, you might want to learn something else than php.
The decision to use many if-statements or one if-elseif-elseif... should not rely on performance, since this decision involves the program flow massively.
I doubt that you can switch from many if-statements to a big if-elseif without loosing functionality.
Its a design question, not a perfomance one.
I made a benchmark if there's a true difference between successive if() and if() then a few elseif()
I put a big string and did about 20 strpos() each time (x100 000) with the two methods and it showed this result :
Try 1 : 0.5094 (including elseif)
Try 2 : 0.6700 (including only if)
There's no doubt. I already knew sucessive elseif() were faster, even though there's a return in the middle ; it's still good to put some statistics in the answer.