-7

Which if statement is faster, with the following conditions. It seems the short-circuit will compile into more instructions then the non-short-circuit.

bool force = false;
bool _isLoadingActual = true;
bool _isLoadingLoadCore = true;

int a = 0, b = 0;

if (force | !(_isLoadingActual | _isLoadingLoadCore))
{
    a = 1;
}

if (force || !(_isLoadingActual || _isLoadingLoadCore))
{
    b = 1;
}
AMissico
  • 21,470
  • 7
  • 78
  • 106
  • 9
    Why don't you just test it? – Evan Trimboli Feb 21 '13 at 02:54
  • 2
    @AMissico If you ran the code you wouldn't have any questions. – Hunter McMillen Feb 21 '13 at 02:56
  • 3
    @AMissico by measuring the time to do it 1000s of times. – kenny Feb 21 '13 at 03:02
  • 1
    @AMissico How do you think that anyone knows which one is faster? You have to compare the execution times. – Hunter McMillen Feb 21 '13 at 03:02
  • 1
    @AMissico If you had said that in the first place, no one would have commented asking why you didn't run it. – Hunter McMillen Feb 21 '13 at 03:09
  • 1
    [obligatory link to performance rant](http://ericlippert.com/2012/12/17/performance-rant/) – Servy Feb 21 '13 at 03:21
  • 1
    @AMissico There are lots and lots of things you can spend your time on that are likely to actually be helpful. If this is honestly the most interesting thing you could inquire about you're not trying very hard to find interesting topics. – Servy Feb 21 '13 at 03:28
  • 2
    @AMissico The spirit of StackOverflow is not to do things you can't be bothered doing, it's about finding solutions to problems you can't find yourself. – TheEvilPenguin Feb 21 '13 at 03:28
  • @AMissico If it would take you hours of digging to find the MSDN docs for the operators you're using, maybe you shouldn't be worrying about micro-optimizations yet. – TheEvilPenguin Feb 21 '13 at 03:52
  • @AMissico No, just making an observation. Either you're vastly overstating the amount of time it would have taken you to find the information Alexi posted, or you literally shouldn't be worrying about micro-optimizations. I don't mean that as an insult, I mean that as advice. – TheEvilPenguin Feb 21 '13 at 03:59
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24866/discussion-between-theevilpenguin-and-amissico) – TheEvilPenguin Feb 21 '13 at 04:18
  • You may also want to read [my answer](http://stackoverflow.com/questions/7331686/why-and-not/7331721#7331721) to a more general question about these operators to understand their differences. In the same question, another user posted [an answer](http://stackoverflow.com/a/7331897/572644) that talks specifically about the differences in performance. – Daniel Hilgarth Feb 21 '13 at 07:45
  • @DanielHilgarth, +1, Awesome. Perfect. I disassembled the code (see comment on Alexei Levenkov answer) and was going to bench test it today if I had a chance. Can convert your comment into an answer, so I can accept it. – AMissico Feb 21 '13 at 15:57
  • @AMissico: This question has already been closed, no more answers are allowed. Unfortunatelly, it has been closed as "too localized". I actually voted to close it as a duplicate to the question I linked to. I think that topic has been discussed in every possible detail in the linked question. – Daniel Hilgarth Feb 21 '13 at 16:00

3 Answers3

3

Here's the fastest solution

a = 0;
b = 0;

Since you already know the boolean variables value.

No seriously.. The 2nd one is probably faster because it can short-circuit, that is if you did not hard code the first boolean to false! Otherwise, short circuiting seems like it must be faster.

Alan
  • 7,875
  • 1
  • 28
  • 48
0

I think the faster is the second code.

if (force || !(_isLoadingActual || _isLoadingLoadCore))
{
    b = 1;
}

Because it's a Short circuit evaluation.

In above code, if force is true then the rest condition (!(_isLoadingActual || _isLoadingLoadCore)) not need to be checked.

Iswanto San
  • 18,263
  • 13
  • 58
  • 79
  • 1
    What about the conditional if, the compiler would have to generate for the short-circuit? – AMissico Feb 21 '13 at 03:01
  • See [this answer](http://stackoverflow.com/a/7331897/572644) for more info on "branch mis-prediction". Bottom line: If all three of those operands are already boolean values (and not methods that return booleans), the short circuiting evaluation is *slower*, because there are multiple branches (`if(force) {} else { if (...) ...`). – Daniel Hilgarth Feb 21 '13 at 07:47
0

Assuming your question is "which one of the | or || does short circuit":

  • || does short circuit - || Operator (C# Reference)

    If the first operand evaluates to false, the second operator determines whether the OR expression as a whole evaluates to true or false.

  • | does not short circuit - | Operator (C# Reference)

    For bool operands, | computes the logical OR of its operands; that is, the result is false if and only if both its operands are false.

So in pure theoretical sense code with || would be faster, also most likely you will not be able to measure impact of the difference in real code.

Note that two statements are not equivalent if computation of operands have side effects (not your case as you have bool variables already).

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • >>Assuming your question is "which one of the | or || does short circuit": << No. I have the bitwise operator on purpose. It seems performing the bitwise operation then the conditional would be faster than performing all the conditionals. – AMissico Feb 21 '13 at 03:09
  • 3
    When used on two bools, `|` is not a bitwise operator, it's a non-short circuting boolean operator. – TheEvilPenguin Feb 21 '13 at 03:14
  • 1
    @AMissico, there is no "bitwise or" defined for bool type in C# - check the link (also I've inlined main part) - it is not "bitwise OR" for `bool`, but rather "non-short-curcuit OR". – Alexei Levenkov Feb 21 '13 at 03:15
  • Isn't logical OR the same as bitwise for boolean values? – AMissico Feb 21 '13 at 03:19
  • 1
    @AMissico, to the extent yes - assuming single bit values. But since it is not explicitly defined as "bitwise" compiler/JIT are free to use the same code as regular "OR" if find necessary. So the only real difference between your 2 statements is if second "OR" need to evaluate right argument for result. – Alexei Levenkov Feb 21 '13 at 03:25
  • Interesting, the IL has a few `brtrue` and `br` statements. It also looks like bitwise `or` is being used in the first `if` statement. Therefore, the bool must be converted to integer values before the bitwise operation. Now I am really interested in testing performance. – AMissico Feb 21 '13 at 04:20
  • Question, `or` "Computes the bitwise OR of two integer values, returns an integer." This means the bitwise OR is explicit because we are in IL and out of C#. Correct? – AMissico Feb 21 '13 at 04:22