468,107 Members | 1,469 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,107 developers. It's quick & easy.

The conditional operator (?:) and performance.

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
Jan 11 '06 #1
9 4504
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" <xm******@hotmail.com> wrote in message
news:acdxf.82576$6K2.51670@edtnps90...
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

Jan 11 '06 #2
Marty <xm******@hotmail.com> wrote in news:acdxf.82576$6K2.51670@edtnps90:
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
Jan 11 '06 #3
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Marty" <xm******@hotmail.com> wrote in message
news:acdxf.82576$6K2.51670@edtnps90...
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

Jan 11 '06 #4
Hi,

"Marty" <xm******@hotmail.com> wrote in message
news:acdxf.82576$6K2.51670@edtnps90...
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.
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 11 '06 #5
Thank you Nicholas, it is very interesting! Somebody was arguing that
it was faster.

Thx :)
Marty

Nicholas Paldino [.NET/C# MVP] wrote:
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.

Jan 11 '06 #6
Thank you everybody for your replies, very interesting.
Have a good day.
Marty

Marty wrote:
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

Jan 11 '06 #7
tjb
Marty <xm******@hotmail.com> wrote:
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.
Jan 11 '06 #8
Which is why I prefer C# over VB.Net.

I keep forgetting about the conditional operator. This thread made an
impression :)

"tjb" <tj*@invalid.invalid> wrote in message
news:1j****************@tjb.invalid.invalid...
Marty <xm******@hotmail.com> wrote:
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.

Jan 11 '06 #9
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

Jan 12 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by TheKeith | last post: by
6 posts views Thread by Mahesh Tomar | last post: by
4 posts views Thread by mux | last post: by
6 posts views Thread by Chris Dunaway | last post: by
5 posts views Thread by paulo | last post: by
4 posts views Thread by digz | last post: by
15 posts views Thread by Nicholas M. Makin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.