-1

Is there any Performance issue in using if (isChecked) vs. if (isChecked == true) in Compiletime or Runtime?

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
Mohamad Shiralizadeh
  • 8,329
  • 6
  • 58
  • 93
  • 3
    **No**.. Both are same. Syntactically and semantically – Shaharyar Jan 01 '15 at 11:34
  • Performance is he same, only `if (isChecked)` has 6 characters less to type on the keyboard – dotnetom Jan 01 '15 at 11:35
  • @Shaharyar: Well no, they're not syntactically the same - they have different syntax trees. – Jon Skeet Jan 01 '15 at 11:35
  • 2
    Of course there is. Whomever reviews that second version is going to lose an hour of his life trying to figure out why on Earth you'd write it that way. That's ten trillion cycles he'll never get back. – Hans Passant Jan 01 '15 at 11:41
  • @JonSkeet So you mean there is a difference between them at the compile time ? – Shaharyar Jan 01 '15 at 11:42
  • @Shaharyar: Well yes, the compiler can tell the difference - you could detect the difference in Roslyn, for example, and suggest a code fix from the long form to the short form. – Jon Skeet Jan 01 '15 at 11:44
  • What is the type of `isChecked`? It looks like it's probably `bool`, but for other types, there is a significant difference. One I've frequently come across is `bool?`, but there are other types where it makes a difference too. –  Jan 01 '15 at 11:55
  • @hvd yes it is bool. – Mohamad Shiralizadeh Jan 01 '15 at 12:00
  • Do you love me? Do you really love me? Do you really really love me? And now, say it in C#: if ( isChecked ) ...; if ( isChecked == true ) ...; if ( isChecked == true == true ) ...; if ( isChecked == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true ) ...; - they all do the same. Just, for me, the shortest versions are easier to understand. – Hans Klünder Jan 01 '15 at 18:36
  • @HansKlünder Is see but Read the Answer please... – Mohamad Shiralizadeh Jan 01 '15 at 18:40
  • I know it makes no difference in performance. I just agree with HansPassant and I think that source code should be clear, which means that pointless complications should be avoided. isChecked is already bool. Every additional `==true` distracts from the fact that isChecked is a perfect expression inside the beaces of an if. – Hans Klünder Jan 01 '15 at 18:45

1 Answers1

8

No performance issue whatsoever. IL generated for both cases is exactly the same and when IL is the same then execution of it will be the same. So no runtime difference.

bool x = true;
if (x == true) // or (x)
    Console.WriteLine("True");

IL_0001:  ldc.i4.1    
IL_0002:  stloc.0     // x
IL_0003:  ldloc.0     // x
IL_0004:  ldc.i4.0    
IL_0005:  ceq         
IL_0007:  stloc.1     // CS$4$0000
IL_0008:  ldloc.1     // CS$4$0000
IL_0009:  brtrue.s    IL_0016
IL_000B:  ldstr       "True"
IL_0010:  call        System.Console.WriteLine

Install LINQPad and try it yourself next time ;)

As for the compile-time, as mentioned in comments, an abstract syntax tree generated will in fact differ. Here's the relevant part of the AST for if(x)

enter image description here

and now for if(x == true)

enter image description here

You can see the difference.

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93