19

I've now seen two different ways to make a boolean returning method:

bool Case1()
{
    if (A)
        return true;
    else
        return false;
}
bool Case2()
{
    if (A)
        return true;
    return false;
}

Which one is faster? Does it make sense to not write else just to save a line, make it clearer, or is there a negligible performance gain?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
user1306322
  • 8,561
  • 18
  • 61
  • 122
  • 7
    "Does it make sense to ... for a negligible performance gain?" -- no! Do what makes sense, not what your CPU executes one nanosecond faster. – John Dvorak Dec 31 '12 at 14:50
  • Questions like this are both too localized and not constructive for SO. Shaving off a few microseconds from a function return from a function is not a major issue in a language in C#, and so it's more a question of style, and not of performance. – Richard J. Ross III Dec 31 '12 at 14:51
  • 2
    @RichardJ.RossIII Since I don't know if there even is some execution time I could save, I feel these kinds of questions should be asked. Besides I might be using them a few thousand times per second, so it could make a difference. – user1306322 Dec 31 '12 at 14:53
  • @user1306322 even at a few thousand times per second, that comes out to be about one nanosecond. Do you realize how trivial one nanosecond is? If you are this performance-adament, switch to a more machine-level language than C#, as your bottleneck will be the VM / JIT compiler, and not your code. – Richard J. Ross III Dec 31 '12 at 14:55
  • `bool Case2() { return A; }` – tckmn Dec 31 '12 at 14:55
  • @RichardJ.RossIII ok, I understand now it's not worth it, but as I said, I didn't know at the moment. – user1306322 Dec 31 '12 at 14:59
  • 2
    @RichardJ.RossIII - For the record I don't agree. Performance matters and while this might not, sloppy thinking does. It is easy for programmers to stop thinking about these issues because they are "to small" and very quickly have a system or program which will never finish. I've seen it time and time again. – Hogan Dec 31 '12 at 16:07
  • 11
    You've written the code both ways. If you want to know which is faster **run it both ways and then you'll know**. http://ericlippert.com/2012/12/17/performance-rant/ – Eric Lippert Dec 31 '12 at 17:01
  • @EricLippert as you can see, and I hope it's obvious enough now, the difference can't be easily determined. I do not have enough knowledge to properly test it and with certainty tell if one is faster than the other. – user1306322 Dec 31 '12 at 17:04
  • 3
    @user1306322: Exactly my point. If the difference is so small that you cannot notice it then stop worrying about it and spend your valuable time working on something that does make a noticable difference in the world. – Eric Lippert Dec 31 '12 at 17:06
  • @EricLippert are you saying that if the difference is small, there is no sense in spending time to find out the exact amount of it? My inner nuclear physicist frowns upon such thinking! – user1306322 Dec 31 '12 at 17:14

5 Answers5

35

No.

Even when we look at their IL code, they have the same IL code, so there is no performance difference between them. Use the one which is more readable for you.

.method private hidebysig instance bool  Case1() cil managed
{
  // Code size       9 (0x9)
  .maxstack  1
  .locals init ([0] bool CS$1$0000,
           [1] bool CS$4$0001)
  IL_0000:  nop
  IL_0001:  ldc.i4.0
  IL_0002:  stloc.1
  IL_0003:  ldc.i4.1
  IL_0004:  stloc.0
  IL_0005:  br.s       IL_0007
  IL_0007:  ldloc.0
  IL_0008:  ret
} // end of method Program::Case1

Look at these pieces of code for their performances;

http://ideone.com/8Sc7Ho --> Memory: 33856 kB

http://ideone.com/MrnaAl --> Memory: 33808 kB

So if you use them even 10.000 times, there is nothing to worry about.

Mert Akcakaya
  • 3,109
  • 2
  • 31
  • 42
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
6

The c# compiler should generate the same IL for these two cases, so there should be no difference in performance. You can always view the generated IL if you are curious what actually happens (attempting to teach how to fish).

IMHO, Case1 is easier to read, which is worth something. My second choice would be return A; (as mentioned in some other answers). If A isn't a bool though, there is an implicit conversion which may be confusing in some cases.

I think readability should win unless you can prove with a profiler that you have a problem.

Community
  • 1
  • 1
WildCrustacean
  • 5,896
  • 2
  • 31
  • 42
  • 4
    "The c# compiler will generate the same MSIL" -- verified? – John Dvorak Dec 31 '12 at 14:52
  • No, not verified. But I would be interested to see why if it didn't. – WildCrustacean Dec 31 '12 at 14:52
  • Too bad I can't vote this up more than once. Case1 is much clearer. Programming languages are written for humans, not compilers, so all things (including performance) being equal, go for the one that is the clearest from a readability standpoint. – David Dec 31 '12 at 14:53
5

There is going to be no(negligible) difference. From a coding standpoint you really should just do:

return A;

But assuming the code was just for an example then I would suggest:

bool Case3()
{
    bool retValue;
    if (A)
    {
        retValue = true;
    } 
    else
    { 
        retValue = false;
    }
    return retValue;
}

This way you are very clear about what is happening, and what value is to be returned. If you need to go back and change the functionality of what the method does, it is much easier.

NominSim
  • 8,447
  • 3
  • 28
  • 38
  • 3
    Yes. It is a very common idea that a boolean expression must reside within an if-statement or loop statement, but a boolean expression is just like any other expression. Nobody would code like this: `if (x==1) return 1; else if(x==2) return 2; else if (x==3) return 3; ...`. Why do this with bools? – Olivier Jacot-Descombes Dec 31 '12 at 15:11
  • Couldn't you just do `return A ? true : false;` then? – baltermia Mar 25 '21 at 13:01
1

Performance wise they are the same. From a good coding practices standpoint, prefer the latter - so that it is clear the function always returns a valid value.

AShelly
  • 34,686
  • 15
  • 91
  • 152
0

They are the same.

If A is false there will be a jump to the return false statement in both cases.

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235