# Which is better: Conditional(Ternary) Operator (?:) or ComparTo() Met

Page 1 of 1

## 7 Replies - 4627 Views - Last Post: 07 March 2009 - 06:11 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=91079&amp;s=011ff7d679a2105acb22622064baf133&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ixios

Reputation: 6
• Posts: 34
• Joined: 01-March 09

# Which is better: Conditional(Ternary) Operator (?:) or ComparTo() Met

Posted 06 March 2009 - 03:53 PM

Hi,
This is my first post in D.I.C.

I was just wondering which method of comparing 2 (int) values is more efficient, using the Conditional Operator, or the CompareTo() Method.

Below, is my code. I've tested it both ways (using ?: and using CompareTo(), and they both work. I am just wondering if there is a difference, more "preferred" way etc.
```		private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
int width = ClientRectangle.Width;
int height = ClientRectangle.Height;
int maxhw;
Pen penf = new Pen(Color.Orange);
penf.Width = 1;

//
// This:	maxhw = width.CompareTo(height) == 1 ? width : height;
// <--OR-->
// This:	maxhw = width > height ? width : height;
//

for (int i = 25; i <= maxhw; i += 25)

{
penf.Color = i % 50 == 0 ? Color.LightSteelBlue : Color.Orange;
penf.Width = i % 100 == 0 ? 2 : 1;
e.Graphics.DrawLine(penf, i, 0, i, height); // draw vertical gridlines
e.Graphics.DrawLine(penf, 0, i, width, i);  // draw horizontal gridlines
}

```

Thanks for taking time to read my post.

Paul

Is This A Good Question/Topic? 0

## Replies To: Which is better: Conditional(Ternary) Operator (?:) or ComparTo() Met

### #2 PsychoCoder

Reputation: 1659
• Posts: 19,853
• Joined: 26-July 07

## Re: Which is better: Conditional(Ternary) Operator (?:) or ComparTo() Met

Posted 06 March 2009 - 04:16 PM

In both attempts you're using the Ternary operator, and for ease of reading I personally would use the second option you have of

```maxhw = width > height ? width : height;

```

That's just personal preference, if you wanted to benchmark it you could create a loop, say 1000 iterations, and do both and see which one performs better

### #3 StCroixSkipper

Reputation: 10
• Posts: 121
• Joined: 23-December 08

## Re: Which is better: Conditional(Ternary) Operator (?:) or ComparTo() Met

Posted 06 March 2009 - 04:18 PM

Both are pretty easy to read. I think the "maxhw = width > height ? width : height;" is slightly easier.

As to which is faster, I'd write a loop using random values for height width and see if I got a difference.

As for the performance, here is the code I wrote:

```using System;
using System.Collections.Generic;
using System.Text;

using System.IO;

namespace TernaryOpTest
{
class Program
{
static void Main(string[] args)
{
// create two random number generators with two different seeds
Random randW = new Random(DateTime.Now.Millisecond);
Random randH = new Random(0);
int i = 0;
DateTime start = DateTime.Now;
while (i++ < 2000000)
{
int height = randH.Next();
int width = randW.Next();
int maxhw = width > height ? width : height;
}
DateTime end = DateTime.Now;
TimeSpan elapsedTime = end - start;
Console.WriteLine("Elapsed Time: {0} ticks", elapsedTime.Ticks);

i = 0;
start = DateTime.Now;
while (i++ < 2000000)
{
int height = randH.Next();
int width = randW.Next();
int maxhw = width.CompareTo(height) == 1 ? width : height;
}
end = DateTime.Now;
elapsedTime = end - start;
Console.WriteLine("Elapsed Time: {0} ticks", elapsedTime.Ticks);

i = 0;
start = DateTime.Now;
while (i++ < 2000000)
{
int height = randH.Next();
int width = randW.Next();
int maxhw = width > height ? width : height;
}
end = DateTime.Now;
elapsedTime = end - start;
Console.WriteLine("Elapsed Time: {0} ticks", elapsedTime.Ticks);

i = 0;
start = DateTime.Now;
while (i++ < 2000000)
{
int height = randH.Next();
int width = randW.Next();
int maxhw = width.CompareTo(height) == 1 ? width : height;
}
end = DateTime.Now;
elapsedTime = end - start;
Console.WriteLine("Elapsed Time: {0} ticks", elapsedTime.Ticks);

}
}
}

```

And here is the output:
Elapsed Time: 800000 ticks
Elapsed Time: 900000 ticks
Elapsed Time: 790000 ticks
Elapsed Time: 880000 ticks

So it appears that the compare takes a little longer which makes sense.

This post has been edited by StCroixSkipper: 06 March 2009 - 04:40 PM

### #4 ixios

Reputation: 6
• Posts: 34
• Joined: 01-March 09

## Re: Which is better: Conditional(Ternary) Operator (?:) or ComparTo() Met

Posted 06 March 2009 - 04:19 PM

Thanks PsychoCoder & StCroixSkipper!

I just realized that I'm using the ?: in both cases. I feel like such a bonehead!

I also like the:
```maxhw = width > height ? width : height;

```

Thanks!

This post has been edited by ixios: 06 March 2009 - 04:21 PM

### #5 coden4fun

• if(coden4fun.NotTired){ ContinueToCode(); }

Reputation: 28
• Posts: 696
• Joined: 27-February 09

## Re: Which is better: Conditional(Ternary) Operator (?:) or ComparTo() Met

Posted 06 March 2009 - 08:03 PM

If they both get the job done I would say it comes down to a matter of preference.

### #6 ixios

Reputation: 6
• Posts: 34
• Joined: 01-March 09

## Re: Which is better: Conditional(Ternary) Operator (?:) or ComparTo() Met

Posted 06 March 2009 - 08:52 PM

Of course there's the ole' switch-aroo:
```switch (width.CompareTo(height))
{
case 1:
maxhw = width;
break;
case -1:
maxhw = height;
break;
default:
maxhw = width;
break;
}

```

But I think I'll stick with the simple Ternary Operator:
```maxhw = width > height ? width : height;

```

Since I'm still new to C#, I just wanted to make sure I'm on the right track. Thank you all very much for your responses. I appreciate it!

Paul

### #7 coden4fun

• if(coden4fun.NotTired){ ContinueToCode(); }

Reputation: 28
• Posts: 696
• Joined: 27-February 09

## Re: Which is better: Conditional(Ternary) Operator (?:) or ComparTo() Met

Posted 06 March 2009 - 10:14 PM

ixios, on 6 Mar, 2009 - 07:52 PM, said:

Of course there's the ole' switch-aroo:

Paul

I'm one of the coders that are fans of one liners over lines of code no matter the simplicity in them both. Even if the one line of code is a little more complex to understand and to integrate I'm willing to take the extra steps and do it well with that one line, than use lines. Not a big fan of lines

This post has been edited by coden4fun: 06 March 2009 - 10:17 PM

### #8 ixios

Reputation: 6
• Posts: 34
• Joined: 01-March 09

## Re: Which is better: Conditional(Ternary) Operator (?:) or ComparTo() Met

Posted 07 March 2009 - 06:11 AM

Quote

I'm one of the coders that are fans of one liners over lines of code

I agree.... I'm a one liner person too... both in coding and jokes!