2

I have been programming a program that tests a value (0<=value<=9) and checks if it is '0' or not. (if not, it'll be from 1-9)

I would like to know whether == or != would be faster/better (I believe this would be in the machine code level?), if there is any other efficient way to do this, or if there is no difference at all.

To put in perspective:

if (value == 0)
{
     //do something
}
else
{
    //do something else
} 

Or...

if (value != 0)
{
     //do something else
}
else
{
    //do something
} 

PS: My program repeats this code many times, so even a small difference can affect my program.

CharithJ
  • 46,289
  • 20
  • 116
  • 131
Mazino
  • 552
  • 6
  • 27

2 Answers2

2

This is called micro level optimization. I doubt you will experience much difference in either case. However, != IL code looks smaller than == IL code.

enter image description here

CharithJ
  • 46,289
  • 20
  • 116
  • 131
1

There is a more general question (not c# specific) already posted and answered. Take a look.

Is the inequality operator faster than the equality operator?

By the way, in my opinon, if you are comparing integer, it doesn't matter what operator you use as they run at the same speed.

Community
  • 1
  • 1
Viet Nguyen
  • 406
  • 3
  • 12