-1

In php, you can use a Nested If structure, or you can combine the conditions using the && method ... are there any known differences in regards to performance/efficiency? (Is one known/established to be faster, or less resource intensive etc.?)

Nested:

if ($X == $Y) {
    if ($N >= $P) {
        if ($tt != true) {
            // do something
        }
    }
}

or Combined with AND:

if ($X == $Y  &&  $N >= $P  &&  $tt != true) {
    // do something
}

I know this appears to be a duplicate of
PHP Nested Ifs vs Single If with Multiple Conditions
but that is because it wasn't really answered (instead people linked to things in Java etc.).

So please don't flag this as a dupe, or link to answers of non-php questions etc.

Community
  • 1
  • 1
theclueless1
  • 123
  • 1
  • 1
  • 11
  • Not only is this identical to the linked question, the accepted answer for that question would be the answer here. Namely, there is no performance difference provided that the code paths are the same. – cmbuckley Jul 01 '13 at 08:31
  • @cbuckley Not really, take a look at opcodes: http://codepad.viper-7.com/bHZbTf and http://codepad.viper-7.com/1lPprc – Leri Jul 01 '13 at 08:33
  • If you're really interested in the opcodes that are generated from your code, use the vld extension. – Ja͢ck Jul 01 '13 at 08:33
  • 3
    BTW, if statement itself will never cause performance issue so go for readability. – Leri Jul 01 '13 at 08:34
  • @PLB I'm not sure how that's different to the [accepted answer](http://stackoverflow.com/a/6032982/283078). – cmbuckley Jul 01 '13 at 08:36
  • 1
    You're better off worrying about the runtime complexity of your algorithms and your SQL queries/database handling. These will *always* dominate your run time, making such small differences, if they exist, negligible. – phant0m Jul 01 '13 at 08:37
  • 3
    **You are obsessing about performance and this will hurt you.** – Jon Jul 01 '13 at 08:37
  • @cbuckley I was answering this: `the code paths are the same.` – Leri Jul 01 '13 at 08:39
  • @PLB I wonder if the jump chain gets optimized in APC / OPcache :) – Ja͢ck Jul 01 '13 at 08:40
  • @PLB there aren't any code paths in your examples :-) Have a look at that answer for what I was referring to. – cmbuckley Jul 01 '13 at 08:40
  • @cbuckley : so you say they are identical - no performance difference at all? That the same amount of memory and processing power is used by both? The only difference is one of presentation? – theclueless1 Jul 01 '13 at 11:39
  • Update : there is a **_tiny_** difference ... the nested form seems to be faster >> http://codepad.viper-7.com/xxPV9d – theclueless1 Jul 01 '13 at 12:06
  • @jon - it's logic like that which creates bloated/sluggish and poor-performance code. Though we may be looking at a 100th of a second ... through in a few iterations, and multiple instances ... and it adds up ... and when you start hitting the 0.5s mark, it can influence users. I'd rather try to get it "right" from the start than have to go through and replace bits and pieces (take for loops, so many use "for($i=0; something; something)" - when you should set that $i=0; outside the for so it isn't set every single time (tiny difference - but how many for-loops in things like WP etc.?) – theclueless1 Jul 01 '13 at 12:07
  • 2
    @theclueless1: You are terribly misguided, and there is no better proof for this than the `for` example you have given (the `$i = 0` is obviously only done once, otherwise how could the loop even work?). Ask any experienced developer and they will tell you to **always profile** before (and after!) attempting to optimize performance, for lots of good reasons (you can google). This is exactly what you are *not* doing here. The ballparks you give (1/100th sec, 0.5s) are nowhere near reality; 1/10000000th sec would be close. I applaud your motives, but your intuition is not yet well developed. – Jon Jul 01 '13 at 12:13
  • @Jon - I can see the logic to what you say - but if you google it, you'll see multiple sites/pages recommending such things regarding for-loops. As for the figures ... you can see them yourself in the link I gave. It's not a fantastic test/comparison, but I did reverse the positions several times to double check equality - nested always came in faster (And they seem to use the same memory ... but I don't know how to check CPU consumption). – theclueless1 Jul 01 '13 at 12:39
  • 1
    @theclueless1: They are recommending taking any unnecessary calculations out of the condition part of the loop, which is right. But that is a completely different story: those calculations are of unknown complexity and doing them might take longer than the loop body itself. Here we are talking about a few CPU instructions (if even that; depending on the language the compiler can generate identical code), which is not only a known and vanishingly small quantity but also is not necessarily in a loop. That's why I said "obsessing" earlier. – Jon Jul 01 '13 at 12:55
  • Some of the examples I've seen specifically point out the initial check and placing it outside - which is why I emulated it (though I do see your point/logic). I wouldn't say "obsessing" - I'd say "overdoing" or "micro-optimising" :D But at least I'm trying to generate clean and efficient code - and I found an answer (even if it is miniscule) :D – theclueless1 Jul 01 '13 at 15:43
  • Sigh - just saw that this had been tagged as Duplicate ... surprise, it got pointed to a Q about Java! It would really help if people took the time to read/understand the question, rather than run off their assumption of the question/questioner. Related Questions = http://stackoverflow.com/questions/6032871/php-nested-ifs-vs-single-if-with-multiple-conditions?rq=1 (Note that AGAIN people point to a JAVA answer/post, not a php one :( ) – theclueless1 Nov 27 '13 at 13:20

2 Answers2

2

Talking of efficiency, I don't think there is any difference in both. Even if one boolean decision is false in one line statement, the remaining decisions won't be evaluated and similar happens in nested if statements.

But readability and clarity definitely matters. Making nested if for each individual condition will make code unnecessarily lengthy whereas many statements in one line will make it difficult to follow as long horizontal statements look bit unclear. I try to keep related conditions as multiple if statements and conditions with different concept as nested if statements.

Shashwat Kumar
  • 5,159
  • 2
  • 30
  • 66
  • That's generally the logic I go for; combine the related stuff, segregate the specific/unusual. But I've been wondering if there is a "cost" ... thus this (ill received) question. – theclueless1 Jul 01 '13 at 12:10
1

If you have performance problems which could potentially be caused by if-clauses, you should not use a scripting language like php, or at least use some sort of caching (e.g. APC).

You will not see any difference going nested vs non-nested in a "normal" situation. If you use millions of ifs you might see a minor difference.

Martin Mandl
  • 721
  • 3
  • 11
  • The difference does seem tiny - it's the accumulated and the compounded impact that concerns me. If I'm looking for multiple conditions on large datasets, it may get more noticeable. If I'm doing that sort of method multiple times, again, it may show. Thus why I thought I'd ask if there is a "better" method. – theclueless1 Jul 01 '13 at 12:13
  • If you see actual differences in your code, you should definitely thinking of using another (more suitalbe) language. – Martin Mandl Jul 01 '13 at 12:15
  • As per the link provided in the initial response; http://codepad.viper-7.com/xxPV9d - there is a difference (though tiny/miniscule) :D – theclueless1 Jul 01 '13 at 12:45
  • Which actually proves my point. If you have 5 million ifs and you do worry about 50 milliseconds, which calculate to 10 nano seconds per if, you definitely should think about using a real-time-capable language. – Martin Mandl Jul 01 '13 at 12:54