Performance difference between multiple conditions in one IF or multiple IF’s separately?

Asked

Viewed 521 times

9

During a change in a source code, I came across the following situation, developed by another developer:

if ( booleano1 || booleano2 || booleano3 )
{
    if( booleano1 )
    {
        //faz algo
    }

    if( booleano2 )
    {
        //faz algo
    }

    if( booleano3 )
    {
        //faz algo
    }
}

Clearly, the developer’s intention was to verify if one of the conditions was true before performing the 3 if separately. The only justification I could find to have been implemented in this way would be a (insignificant?) performance gain.
So my question is: is there any improvement in performance when using various conditions in the same if in relation to implementing several if separately?

  • 6

    That looks like microtimization neura huh

  • It is, in my conception, even if there was a gain, it would be insignificant even for being a primitive Java operation, but this implementation left me with the doubt if there really is a difference.

  • @Wallacemaxters Depends on what the boolean expression does. If the developer repeated the logic in each IF the code can be beeeem slower and no need. I’ve seen it a lot in the name of saving a variable.

  • 1

    It seems to me a case of a skeptical developer. After all, these languages today are not very reliable. Will the variable change from an hour to another without explanation? To ensure even more I would do so: if (booleano1 && booleano1). Always check twice if you want to be sure. Better safe than sorry.

  • @utluiz sério ? (booleano1 && booleano1) ? mds kkk

  • 1

    @Gabrielrodrigues To be better just doing booleano == true && booleano == true. Prevented programmer counts for two. D

  • @utluiz usa === not to do uhauha type conversion

  • 1

    what the world would be without the irony hahahahahaha

Show 3 more comments

2 answers

7


This code certainly performs less than it should. There is no reason for the if external, does not change any logic, or there is gain, on the contrary, it will certainly make redundant comparisons.

If there was code inside this if in addition to the three ifinternal, then would have some logical sense.

If they were separated, that is, if you were comparing only the if external with the set of the three ifinternal s running separately would be difficult to compare because they would be running something totally different. The if with relational operators will execute once if one of them is true. If you put the three ifs each of them will be executed depending on the condition of each.

If they do not perform the same thing, that is, if they do not produce the same result, they cannot be compared.

If there were a situation where they could gain it would be minimal or non-existent. It could be until it depended on the compiler (even on his version), so it is not something that can be affirmed, what is worth one day may not be true the other.

Don’t worry about it.

  • 1

    Complementing the answer of the bigown, in some computer courses there is the discipline of Algorithm Complexity, in which you learn to measure how expensive a logic can be or not. If it were to be placed on top of that code it would have a High Complexity, besides unnecessary, because: It is executed a comparison that is again executed ifà if If in the comparisons below there was one more clause that verified a second value and this value depended on the order of the validations made previously this logic may be acceptable, but thus spent processes.

  • You’re talking specifically about cyclomatic complexity, right?

  • Exactly bigown

1

The performance gain would be insignificant for your case, It is not recommended to overuse the IFs, because it makes your code difficult to read and maintain, so it’s not good practice. Can increase the complexity of the code, increases the difficulty in understanding and when making future maintenance.

Source: Why in some if’s situations are considered bad?

  • 2

    As @bigown’s reply says, there is no gain in any situation as the external IF is logically redundant.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.