The conditional operator (?:) and performance.

  • Thread starter Thread starter Marty
  • Start date Start date
M

Marty

Hi,

Does using the the conditional operator (?:) instead of the common "if"
statement will give a performance gain in a C# .NET 2003 application
(even in C# .NET 2005?).

What is the advantage of using it in C# other than typing shorter if ?

Thanks,
Marty
 
I think some people might find it easier to read since its shorter notation.
Other then that I am not aware of any performance benefits, but I could be
wrong on that.
 
Marty said:
Does using the the conditional operator (?:) instead of the common "if"
statement will give a performance gain in a C# .NET 2003 application
(even in C# .NET 2005?).

Only in the sense that it improves YOUR performance (typing, that is.)

-mdb
 
Marty,

It depends (somewhat) on the mode you compile it in. In debug mode,
using the conditional operator will reduce your code profile. When checking
the IL, a simple function which had nothing but a conditional operator in it
(with some setup), next to the same function using an if statement, the
function with the if statement had 10 more lines of IL in it.

In release mode, the IL was virtually identical with the exception of
one line.

Now, what does this mean? Pretty much nothing. I would not get in a
tizzy over 10 lines of code in debug version being compiled by the JIT,
which is going to optimize it anyways, and probably produce similar code
with similar performance profiles.

To be honest, if someone told me that one was more performant over the
other, then I would laugh. I mean, it's VERY unlikely, and if there is a
difference as a result of this, it is going to be SLIGHT and most likely
insignificant.

Hope this helps.
 
Hi,

Marty said:
Hi,

Does using the the conditional operator (?:) instead of the common "if"
statement will give a performance gain in a C# .NET 2003 application (even
in C# .NET 2005?).

I bet that the code generated is the same.

I do use it exclusively as a conditional assignment:

DateTime dt = DataReaderRow["theDate"] == DBNull.Value? DateTime.MinValue :
Convert.ToDateTime( DataReaderRow["theDate"] );

I never use it like:
a>b? CallA() : CallB();

I guess it's just preference.
 
Thank you Nicholas, it is very interesting! Somebody was arguing that
it was faster.

Thx :)
Marty
 
Marty said:
What is the advantage of using it in C# other than typing shorter if ?

It can be used to achieve conciseness, without loss of clarity (although it
*can* be abused, as is the case with other useful language features, such
as polymorphism). For example, the following two snippets are semantically
equivalent:

1:
int x;
if (someCondition) {
x = FirstValue;
} else {
x = SecondValue;
}

2:
int x = someCondition ? FirstValue : SecondValue;

The first seems overly verbose when compared to the second, which is short
and to the point.
 
Which is why I prefer C# over VB.Net.

I keep forgetting about the conditional operator. This thread made an
impression :)
 
The conditional operator is also good for parameter lists:

int num = CountMessages();
Console.WriteLine(
"You have {0} new message{1}.",
num,
num == 1 ? "" : "s");

Jesse
 
Back
Top