473,323 Members | 1,550 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 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 4748
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.