BLOG.CSHARPHELPER.COM: Compare the speeds of the conditional (ternary) ?: operator and the if-else statement in C#
Compare the speeds of the conditional (ternary) ?: operator and the if-else statement in C#
The conditional operator ?: looks confusing enough that some programmers assume it must be more efficient than a comparable but longer if-else statement. This example uses the following code to compare the speeds of the conditional operator and an if-else statement.
// Trials using if.
x = 1;
start_time = DateTime.Now;
for (long i = 0; i < num_trials; i++)
{
if (x % 2 == 0) x = x + 1;
else x = x + 3;
}
elapsed = DateTime.Now - start_time;
lblIfTime.Text = elapsed.TotalSeconds.ToString("0.00") + " seconds";
Refresh();
// Trials using ?:.
x = 1;
start_time = DateTime.Now;
for (long i = 0; i < num_trials; i++)
{
x = (x % 2 == 0) ? x + 1 : x + 3;
}
elapsed = DateTime.Now - start_time;
lblConditionalTime.Text = elapsed.TotalSeconds.ToString("0.00") + " seconds";
Refresh();
The code records the current time in the start_time variable and then enters a loop. The loop determines whether the variable x is odd or even and adds either 1 or 3 to it.
When the loop finishes, the program subtracts start_time from the current time and displays the number of seconds that have elapsed.
The program then repeats the same steps using the conditional operator instead of an if-else statement.
You can see from the figure that the difference between the two approaches is negligible. In this trial, after 100 million tests the difference in time was only 30 milliseconds. Variations in the operating system are more significant so if you run the tests several times you will get different results and sometimes the if-else statement will be faster.
The moral is you should use whichever of these statements seems easier to read and understand and not worry about performance. For a very short statement, you may find ?: more compact than if-else. Often ?: is confusing so I usually use if-else instead.
(Note that it's a good thing that the performance of the two statements is the same. It means the C# code is being converted into comparable code before execution. It would be bad if Visual Studio compiled one of these two equivalent statements into something that was less efficient than the other. But it's also useful to check.)
5/24/2012 2:32 PM
Sam wrote: There are a few things wrong with this "test".
a) You can't use DateTime.Now to reliably benchmark performance. The Stopwatch class was designed for benchmarking; use it instead.
b) The ?: operator will create equivalent IL to if-else, so how could one possibly be faster than the other?
I suggest you read Eric Lippert's blog as he discussed these types of issues before. Reply to this
5/26/2012 6:56 AMRod Stephens wrote: a) You can if you perform a lot of trials. StopWatch is wonderful but you can do even better if you really care. But when you're talking about a difference of a few milliseconds (or even microseconds in some cases) out of 2.5 seconds, the difference isn't really significant. If you want to try to time a single test, then yes you need a better timer. But in that case the task switching of the operating system can totally blow your measurement away.
b) True. If you feel like reading the IL, look at http://www.reflector.net/. If you really dig into it, you can see how the performance of any set of related statements should compare. Reply to this
There are a few things wrong with this "test".
a) You can't use DateTime.Now to reliably benchmark performance. The Stopwatch class was designed for benchmarking; use it instead.
b) The ?: operator will create equivalent IL to if-else, so how could one possibly be faster than the other?
I suggest you read Eric Lippert's blog as he discussed these types of issues before.
Reply to this
a) You can if you perform a lot of trials. StopWatch is wonderful but you can do even better if you really care. But when you're talking about a difference of a few milliseconds (or even microseconds in some cases) out of 2.5 seconds, the difference isn't really significant. If you want to try to time a single test, then yes you need a better timer. But in that case the task switching of the operating system can totally blow your measurement away.
b) True. If you feel like reading the IL, look at http://www.reflector.net/. If you really dig into it, you can see how the performance of any set of related statements should compare.
Reply to this