1

I'm trying to get better in PHP foundations and I have run this simple test on php 7.0.4:

<?php

     function am_I_executed(){
        echo "yes I am executed\n";
        return true;
     }

    echo "test1: \n";
    if (false && am_I_executed()) {
         echo "this should never be displayed \n";
    }

    echo "test2: \n";
    if (am_I_executed() && false) {
        echo "this should never be displayed \n";
     }

The result is:

test1
test2
yes I am executed

Before trying this I thought that the compiler would just skip this if statement because it should always give false, doesn't matter what's the return of am_I_executed() and that there would be no difference between test1 and test2

I was obviously wrong.

I believe that in real life experience it might happen a situation like this one:

if(algorithm1() && algorithm2()){}

but in some occasion it might be hard to understand which is the lighter algorithm and then decide in which order they should be listed in the if statement.

How do you tackle this problem?

wwr
  • 327
  • 1
  • 4
  • 11
  • 4
    See: http://stackoverflow.com/q/5694733/3933332 – Rizier123 Mar 20 '16 at 13:32
  • I believe the condition will be evaluated from left to right. The "test 1" doesn't call the `am_I_executed()` because the interpreter encounters `false` first. – frz3993 Mar 20 '16 at 13:43
  • @frz3993 yes it seems like that but that means that the order matters in terms of performance. What to do when the evaluated algorithms are somehow encapsulated and I have no idea which one in heavier? Is testing the only solution? – wwr Mar 20 '16 at 13:45
  • If it's _hard to understand which is the lighter algorithm and then decide in which order they should be listed in the if statement,_ even for humans, then how is PHP supposed to do this automatically? By the way, the feature you've just discovered is called *[short-circuit evaluation](https://en.wikipedia.org/wiki/Short-circuit_evaluation)* and is common to many languages, not just PHP. – Alex Shesterov Mar 20 '16 at 13:52
  • 1
    Let's take `if (function1() && function2())` for example. You wouldn't know if any of the function will return false or true. If you know one of them will always return `false`, you don't need the condition. Besides, I don't think the performance impact will be significant. – frz3993 Mar 20 '16 at 13:54
  • @Rizier123 wouldn't be a dupe of http://stackoverflow.com/q/5694733/? – Funk Forty Niner Mar 20 '16 at 14:25
  • 1
    @Fred-ii- Not sure, since OP is kinda asking how to find the "lighter algorithm" in these occasions, but changing the function call order also changes the logic of the code. – Rizier123 Mar 20 '16 at 14:27
  • 1
    @Rizier123 We'll just let the question/reviews take its destined route then ;) – Funk Forty Niner Mar 20 '16 at 14:28

1 Answers1

2

I think you should do test and calculate multiple execution times between 2 algorithms and do a statistical analysis of their execution time in order to decide which is lighter.

An another and faster way, is to read the code of these 2 functions and calculate their complexity.

The lightest complexity (or the shortest statistically time) goes first.

Dimitrios Desyllas
  • 9,082
  • 15
  • 74
  • 164