views:

390

answers:

10

im iterating through the array and sorting it by values into days of the week.

in order to do it i'm using quite many if statements. does it make any difference for the processing speed if i use many ifs versus elseif statements?

+12  A: 

I doubt that a micro optimization like this will make a measurable difference in your code.

Your sorting algorithm is more likely to be the source of a performance problem. Which sorting algorithm you choose will be critical, not many "ifs" versus "else if".

UPDATE:

The points made by others about "else if" being a better choice, due to its early exit and exclusive logic characteristics, suggest that it should be preferred over "if" in this case.

But the point about algorithm choice still stands - unless your data set is very small.

It's obvious that O(log n) would be better than O(n^2), but size of dataset matters as well. If you have only a few elements, you might not notice the difference. In that case, coding an inefficient method in the cleanest, most readable, most easily understandable at a glance could be your best bet.

duffymo
Most sorting algorithms use nested loops or recursion and a lot of comparisons to sort a given set. Depending on how many comparisons he is doing in this critical part of the code, early outs and skipping unnecessary compares could result in a significant performance increase.
Yannick M.
Agreed, good point, Yannick.
duffymo
Wouldn't an if-else aid readability also, as well as giving the compiler a decent hint?
James B
Yes, nice point.
duffymo
+1  A: 

else if would be faster in the sense that you compare until you hit a condition that resolves to true, and you skip the rest of the ifs.

Also consider reordering the compares in order of descending frequency.

And using the switch statement depending on the datatype of the object you are comparing.

However at this point, as duffymo has suggested, you would be micro optimizing. The performance gain will never be as significant if you haven't chosen the right sorting algorithm for the job first.

Yannick M.
+3  A: 

To be honest I don't think it would matter which way you do it in terms of performance, I doubt you would see any difference. I would recommend using a switch statement which isn't a performance enhancment, simply syntactically nicer:

switch ($day) 
{
    case "Monday":
        // do something with Monday
        break;
    case "Tuesday":
        // do something with Tuesday
        break;
    case "Wednesday":
        // do something with Wednesday
        break;
}
James
+1 Defiantly use Switch!
Shadi Almosri
A: 

Have you seen in your profiler that branching takes a significant amount of time in your code?

erikkallen
+7  A: 

Yes, use an else if, consider the following code:

if(predicateA){
  //do Stuff
}
if(predicateB){
  // do more stuff
}

of

if(predicateA){
  //
}
else if(predicateB){
  //
}

in the second case if predicateA is true, predicateB (and any further predicates) will not need to be evaluated (and so the whole code will execute faster), whereas in the first example if predicateA is true, predicateB will still always be evaluated, and you may also get some unexpected suprises if predicateA and predicateB are not mutually exclusive.

James B
It would need to be a substantial amount of conditions to actually see a performance difference however +1 as if else would be the proper way anyway.
James
A: 

If the values are integers you may achieve an optimisation by using a table lookup. E.g. say you have 256 values that map into 7 days somehow, you could set up an array with 256 cells and each cell contained the day of week you wanted. Then instead of:


if ( value == 0 ) {
  dayofweek = 1;
} else if ( value == 1 ) {
  dayofweek = 2;
} else if ( value == 2 ) {
  dayofweek = 3;
} else if ...

.. you could have..


dayofweek = lookuparray[value];

Of course, if you use this technique, then you should check the bounds of value first.

PP
+1. Don't know why this was downvoted, it's actually quite good advice.
paxdiablo
A: 

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).

MaxVT
A: 

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.

BeowulfOF
+1  A: 

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.

alt text

mnml
thanks for the link
ondrobaco
A: 

I would put another vote in for opting for a switch() statement instead.

Martin Bean