Greater Than vs Less Than vs Equals in C#

Greater Than vs Less Than vs Equals in C#

As a programmer, have you ever had a situation where there’s a variable that could be 1 of 2 or maybe 3 values? I came across code where there was a variable “x”, which could be -1, 0, or 1. In testing for values, the code had simple logic which made sense:

While the above code works, since we know the finite number of values x can be, what if we tweaked it to the following:

Greater Than vs Less Than vs Equals.

 

We don’t give much thought to it. Both code samples are just as easily readable above.

 

So that’s when this Curious Consultant started wondering what’s faster?

  • Greater than ( > )
  • less than ( < )
  • or equals ( == )

in C# logic constructs.

 

Does it even make a difference?

 

Let’s find out!

 

The Nuts and Bolts

There isn’t anything complicated or fancy about this test.

 

It is written in Visual Studio 2017 targeting .Net Framework version 4.7.1 x64. The source code is available at the end of this blog so you can benchmark it on your own system.

 

In a nutshell, the code does the following:

  1. Loops a specified number of times using the comparison operator
  2. If it meets the comparison criteria, the running total is incremented by 1.

 

Assumptions:

  • No exception handling as no exceptions should occur

 

The exe file was run under Windows 10 Professional 64-bit with 32GB of memory.

 

The test was run for the following number of comparisons:

  • 100
  • 1,000
  • 10,000
  • 100,000
  • 1,000,000
  • 10,000,000
  • 100,000,000

 

Line Up the Starting Positions

These tests were run a few times, but to be honest I didn’t feel like documenting every single one. The results were all similar, so I just took the times from one of the middle runs.

 

All times are indicated in seconds.milliseconds format. Lower numbers indicate faster run time performance.

Winners are highlighted in green; there are no points for second place.

 

The Results Are Interesting, but Consistent:

Have a look over the results. Each set is consistent. That is,

  • if x is 1, the test x == 1 performed better than x > 0
  • if x is -1, the test x == -1 performed better than x < 0

 

Number of Runs

 

100

1,000

10,000

100,000

1,000,000

10,000,000

100,000,000

where x is -1

x < 0

00.0000004

00.0000018

00.0000145

00.0001435

00.0014923

00.0161081

00.1599993

x == -1

00.0000004

00.0000014

00.0000108

00.0001053

00.0012375

00.0118365

00.1089501

 

where x is 1

x > 0

00.0000004

00.0000017

00.0000165

00.0001434

00.0014903

00.0156466

00.1737996

x == 1

00.0000005

00.0000014

00.0000264

00.0001054

00.0010915

00.0121319

00.1103028

 

In Summary:

While the time differences probably aren’t significant enough to notice running an application, as the number of iterations gets higher, “==” definitely outperforms it’s respecting “<” or “>” comparison.

 

Is x > 0 any easier or harder to read or maintain than x == 1?

 

No. User preference.

 

However, if there’s a need to squeeze every last little microsecond possible, it’s best to stick with the “==” comparison operator where it makes sense.

 

The Code:

 

David Lozinski