0

I got a question about PHP/in general performance difference in the order of if / elseif / else statements. It makes sense to run a if / elseif / else statements in order of the most often true statement at first for less checks, to improve performance.

Now my question is, what about simple if/else statements without elseif? Does it make a difference if the statemen if is false and it jumps to the else case which actualy doesnt check anything since its the default? Or is there also an additional time added to the runtime by jumping to the else instead of inside the if statement? And if so how much of a difference does it make?

Edit:

@MonkeyZeus thanks for that term.

Yes that is part of what i mean. But in case of this:

if(x>10) {y = 10}
else {y = 0}
//or
if(x!>10) {y = 0}
else {y = 10}

does it make a difference if it jumps to the else since it checks always one statement? So if lets say case 1 happens more often, i go that way, if case 2 happens more often, i go that way with the code. The result is the same but in one way it jumps 80% of the time in the if case in the other case it jumps 80% in the else case.

Dapp Future
  • 165
  • 1
  • 12
  • One CPU tick more or less will not make a difference, but if you have CPU extensive operations in your condition or in your code block then you need to consider when it should be executed. – Dharman Jun 24 '19 at 18:40
  • 2
    If there is any difference, it will never have any measurable impact on your application. If you want to improve performance, run a profiler to see where your bottlenecks actually are. – Alex Howansky Jun 24 '19 at 18:43
  • You'll see it in order of comparisons maybe if `$x` is more frequently false: `if(someSlowFunc() && $x)` slower than `if($x && someSlowFunc())` because if `$x` is false func will not run. – AbraCadaver Jun 24 '19 at 18:43
  • Well i know in a single call its no big difference, but what if i got a loop with 10000 calls, then 0.01ms can have a impact. – Dapp Future Jun 24 '19 at 18:49
  • You are looking for "short-circuit evaluation" and yes there is absolutely a performance benefit to be gained depending on the number of iterations. https://en.wikipedia.org/wiki/Short-circuit_evaluation. It's not just a matter of jumping into an `else{}` but the order of comparison inside of the `if(){}` can make a big impact as well. – MonkeyZeus Jun 24 '19 at 18:51
  • Please check my edited question. – Dapp Future Jun 24 '19 at 19:02

2 Answers2

1

As others have stated, proper benchmarking is your best way to know from a practice point of view. In theory though, there shouldn´t be any difference between your example (or any if else example) because among the many code execution optimizations, one of those is branch prediction. This will make sure that given enough iterations in a row going through the same branch of the if, execution will start predicting that further iterations will also go through that branch and start pre-executing the code from it.

As for a else if case, I´m not entirely sure but I would think it is similar if the cache prediction is done for each line of code.

juvian
  • 15,875
  • 2
  • 37
  • 38
-2

At the end of the day, you are simply asking "benchmark this for me, plz."

So here you go:

PHP 7.3.5 @ http://sandbox.onlinephpfunctions.com/

<?php
$x = 1;
$iterations = 10000000; // 10 million

$start = microtime(true);
for( $i = 0; $i < $iterations; ++$i )
{
}
echo microtime(true) - $start; // 0.071332216262817 seconds
echo PHP_EOL;

$start = microtime(true);
for( $i = 0; $i < $iterations; ++$i )
{
    if( $x < 10 ){}
}
echo microtime(true) - $start; // 0.10689616203308 seconds

It takes about 40% more time to work through an unneeded if(){}

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • 1
    I don't think the question is whether or not to have an `if`. It is more about whether or not the order of the contents of the `if` and `else` blocks matter. – Patrick Q Jun 24 '19 at 19:07
  • Thank you for that website / code. Yes as Patrick said the question was a little different but close to the answer. I tested the difference between 100% if = true and 100% else and it made ~5% difference. Thank you. – Dapp Future Jun 24 '19 at 19:09
  • Maybe this benchmark would answer more questions: https://3v4l.org/8tOV2 ...there is no difference between the true or false condition. – Dharman Jun 24 '19 at 19:10
  • @PatrickQ The answer still stands: BENCHMARK IT. It's going to differ between PHP versions and possibly even between CPUs. The difference is probably negligible and readability should be a higher priority. – MonkeyZeus Jun 24 '19 at 19:13
  • @MonkeyZeus I wasn't arguing any of that. Personally, I wouldn't have taken the time to write an answer for this at all. I was just saying that what you _did_ take the time to write up didn't really match what the OP was asking. I completely agree that unless OP works at Google, Amazon, etc, then this is a case of premature- / over- optimization – Patrick Q Jun 24 '19 at 19:16
  • @MonkeyZeus Thats why i was asking this, i m new to optimization of code, i m just in it for 2 days now, still doing research what optimization makes sence. – Dapp Future Jun 24 '19 at 19:28
  • 2
    @DappFuture I understand the premise of your question but without knowing what kind of applications you are building it's near impossible to properly answer your question. After 10 million iterations the difference in my example is honestly negligible. It's up to you to benchmark and move on. `microtime(true)` is the only useful piece of information in my answer since the rest will always be user and environment specific. Also, the `++$i` is some well-known low-hanging optimization fruit; don't use `$i++`. – MonkeyZeus Jun 24 '19 at 19:33