1

the question is more of a performance issue than elegancy, i speek for my self, but ...

having both options to implemet , which is faster for the cpu to compute, or if at the end it is the same (i tend to think it is, cause the condition is (x<10) same )

    public int DidLogcount = 0; // DidLogCount is raised by +1 every time we deside, then condition is met 
    public bool MoreLogsAllowed()
    {
        if (DidLogcount < 10) return true;
        else return false;
    }

vs

    public bool MoreLogsAllowed()
    {
       return DidLogcount < 10;
    }

we most of time supposeto check if it's not null but, if we have to , it will incloud both cases, so guess i just narrowed it down (?) if i didn't miss any other issue.

i'll appriciate the correct answer. thx.

Rediting : I just wanted to mark correct answer but did a refresh on the page which loaded 3 more ...

going to wait form more votes ...and for now, I really want to thank you all! for sharing your knowledge , it did go through my mind though , the optimization issue by the compiler , so...having those printouts shows something although little difference is little by it's self, but when added to a pile of conditions , it is less little , I wouldn't say big unless we're talking about a real complex app. still a performance issue is never a small issue i should say, along with logic and readability thanks to @Steve & @Nick for actually testing it for us .

LoneXcoder
  • 2,121
  • 6
  • 38
  • 76
  • similar to [this previous SO question][1] [1]: http://stackoverflow.com/questions/2977365/which-is-clearer-form-ifvalue-or-ifflag-value/2977394#2977394 – Charles Bretana Aug 30 '12 at 12:45

6 Answers6

5

I am not sure if there is going to be any performance difference, and if any, its going to be negligible. But the 2nd case is more elegant.

Habib
  • 219,104
  • 29
  • 407
  • 436
4

Tested with LinqPAD.

int DidLogcount = 5;
void Main()
{
    MoreLogsAllowed();
    MoreLogsAllowed2();
}

public bool MoreLogsAllowed() 
{ 
    if (DidLogcount < 10) return true; 
    else return false; 
} 

public bool MoreLogsAllowed2() 
{ 
    return (DidLogcount < 10); 
} 

and produces this IL code

IL_0000:  ldarg.0     
IL_0001:  call        UserQuery.MoreLogsAllowed
IL_0006:  pop         
IL_0007:  ldarg.0     
IL_0008:  call        UserQuery.MoreLogsAllowed2

MoreLogsAllowed:
IL_0000:  ldarg.0     
IL_0001:  ldfld       UserQuery.DidLogcount
IL_0006:  ldc.i4.s    0A 
IL_0008:  bge.s       IL_000C
IL_000A:  ldc.i4.1    
IL_000B:  ret         
IL_000C:  ldc.i4.0    
IL_000D:  ret         

MoreLogsAllowed2:
IL_0000:  ldarg.0     
IL_0001:  ldfld       UserQuery.DidLogcount
IL_0006:  ldc.i4.s    0A 
IL_0008:  clt         
IL_000A:  ret    

The second version not only is more elegant but it seems to produce shorter code.
Just to be sure that this is not a LinqPAD introduced difference, I have created a small console application with Visual Studio 2010 and compiled with the default settings in release mode.
Then I used ILDASM to see the IL colde.
I confirm that the code above is the same code produced with the compiler.
Of course the difference is negligible, but the two versions don't produce the same IL code.

Steve
  • 213,761
  • 22
  • 232
  • 286
  • 1
    That looks to be a completely unoptimized, debug-style compile? – utopianheaven Aug 30 '12 at 12:44
  • @utopianheaven you are right. LinqPAD was not optimized, I have set the correct option now and I update the code, but there are still differences – Steve Aug 30 '12 at 12:49
2

There is no elegance in first example, as for me. Moreover I think compiler will optimize first case, so there is nothing to talk about performance. For me, second example is more concise, shorter and readable.

Ilya Ivanov
  • 23,148
  • 4
  • 64
  • 90
2

I would be very surprised if there was a difference in performance between the two alternatives. I too find the second alternative more elegant and I would even omit the parentheses.

With more complex conditions however, especially if function calls are involved, having the two return statements on different lines makes it a little more convenient to find out what is going on while stepping through your code with a debugger.

Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55
2

Any performance differences would probably be negligible and highly dependant on the of the .NET JIT compilation - but looking at an x86 example the comparison ends up as:

                if (DidLogcount < 10) return true;
... skipped DidLogCount call...
    00000020  mov         dword ptr [ebp-4],eax 
    00000023  cmp         dword ptr [ebp-4],0Ah 
    00000027  jge         00000037 
    00000029  mov         eax,1 
    0000002e  and         eax,0FFh 
    00000033  mov         esp,ebp 
    00000035  pop         ebp 
    00000036  ret 
                else return false;
    00000037  xor         eax,eax 
    00000039  mov         esp,ebp 
    0000003b  pop         ebp 
    0000003c  ret 

vs

            return (DidLogcount < 10);
    ... skipped DidLogCount call...
0000001f  mov         dword ptr [ebp-4],eax 
00000022  cmp         dword ptr [ebp-4],0Ah 
00000026  setl        al 
00000029  movzx       eax,al 
0000002c  mov         esp,ebp 
0000002e  pop         ebp 
0000002f  ret 

The later would probably be slighlty quicker - but again it would depend on exactly how your CPU processes those instructions.

The best answer is to write a test application to measure it!

Nick Jones
  • 6,413
  • 2
  • 18
  • 18
  • I'm trying to work out in the first example what the point is of "0000002e and eax,0FFh" - any help would be appreciated! – Nick Jones Aug 30 '12 at 13:12
  • i tend to compile most of my code into x64 what is the main issue regarding 64 vs x86(32bits) if u had to 1 or 2 that are most clearest effect , is it like in language(abc) when u can use 48 letters insted of half the sentences you can make out of ? – LoneXcoder Aug 30 '12 at 13:37
  • so if i would belive u , which is kind'a hard . not allways it's faster doing it on x64 i would think that not every model increse implies that its better than it's predecessor but i was sure that moving on to x64 from x86 or more notesebly 32bit to 64bit is a win win 99.999999 % if it's not 100 – LoneXcoder Aug 30 '12 at 13:52
  • i guess that multiple threds and multiple cores are more performance responsible than just the bits . – LoneXcoder Aug 30 '12 at 13:56
0

You don't have difference between two fragments because the most important is MSIL, after compilation and in this case , the compiled code are identicals.

Code 1 C# -> MSIL 1 -> Code natif 1

Code 2 C# -> MSIL 1 -> Code natif 1
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51