472,989 Members | 2,927 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 software developers and data experts.

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 4729
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: neblackcat | last post by:
Would anyone like to comment on the following idea? I was just going to offer it as a new PEP until it was suggested that I post it here for comment & consideration against PEP 308. I'm far...
4
by: TheKeith | last post by:
I just wrote the following script for something I'm working on: ---------------------------------------------------------------------------- ------------------- <html> <head> <script...
6
by: Mahesh Tomar | last post by:
Please see the code below :- void func() { unsigned char x,y,z=1; (z==1) ? (x) : (y) = 1; /* Compiles OK */ ((z==1) ? (x) : (y)) = 1; /* Compiler generates an error "Variable expected" */ }
4
by: mux | last post by:
Hi I found out that the following piece of code throws an error. 1 #include "stdio.h" 2 3 int main() 4 { 5 int a,b; 6 a= 10;
6
by: Chris Dunaway | last post by:
Consider this code (.Net 2.0) which uses a nullable type: private void button1_Click(object sender, System.EventArgs e) { DateTime? nullableDate; nullableDate = (condition) ? null :...
5
by: paulo | last post by:
Can anyone please tell me how the C language interprets the following code: #include <stdio.h> int main(void) { int a = 1; int b = 10; int x = 3;
4
by: digz | last post by:
Hi, I am having a lot of trouble setting conditional breakpoints in gdb, here is a simple example... #include<string> #include<iostream> using namespace std; void func(string& s){ cout <<...
15
by: Nicholas M. Makin | last post by:
I was just thinking that I understood the conditional operator when I coded the following expecting it to fail: int a= 10, b= 20, c= 0; ((a < b) ? a : b) = c; // a=0 a=20; b= 10; ((a < b) ? a...
3
by: somenath | last post by:
Hi All, I have one question regarding the conditional operator. In the draft C99 standard it is mentioned that "1 The following are the sequence points described in 5.1.2.3: -- The call to a...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.