1

Simple question (I think): Which of these pieces of code would execute faster in C#?

newSpeed = newSpeed > maxSpeed ? maxSpeed : newSpeed;

or

if (newSpeed > maxSpeed)
{
    newSpeed = maxSpeed;
}
  • 6
    It doesn't matter. – SLaks Sep 28 '12 at 17:22
  • 1
    Jeff Atwood on micro-optimization: http://www.codinghorror.com/blog/2009/01/the-sad-tragedy-of-micro-optimization-theater.html – McGarnagle Sep 28 '12 at 17:23
  • 2
    it would've taken you all of 2 minutes to write a test project to actually time this... – Mike Corcoran Sep 28 '12 at 17:28
  • @SLaks - why do you say that? because the times for each are so quick that it's irrelevent which is faster? – whytheq Sep 28 '12 at 21:30
  • 1
    @whytheq he's saying that because this doesn't look like a micro-optimization, but look more like a nano-optimization.... In general, if you want to optimize, you optimize when and where is needed. If someone need an optimization like this he/she better start by writing the application in another language – Fabio Marcolini Oct 31 '13 at 19:38

3 Answers3

8

I am guessing the second would be faster, in some cases, as it does not always do an assignment, whereas the first always does an assignment.

E.g., when newSpeed <= maxSpeed, no assignment is done, only a comparison.

D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
  • 3
    This, but also, ternary assignments are simply turned into the IL equivalent of if-else statements, and the compiler may well figure out that the "else" block is redundant and get rid of it. – KeithS Sep 28 '12 at 17:28
  • @KeithS That sounds likely. Perhaps only faster to compile then ;) – D'Arcy Rittich Sep 28 '12 at 17:31
  • Makes me wonder, though, if `newSpeed` is a property with some other code in its setter, it would be wrong to get rid of it. The programmer may be resetting the value just to trigger this other code, for example. (Not saying that's good programming practice, just an example of where the compiler could have an undesirable effect on how your code runs.) – D'Arcy Rittich Sep 28 '12 at 17:34
  • The compiler can tell the difference between a field and a property. I'm not saying it'd even optimize this, but it looks obvious enough. – KeithS Sep 28 '12 at 17:38
  • @KeithS Yes, I meant this distinction was not made in the question, so a definitive answer perhaps not possible. – D'Arcy Rittich Sep 28 '12 at 17:42
  • I just checked - the IL is, in fact, different, regardless of whether a field or property. – D'Arcy Rittich Sep 28 '12 at 17:55
0

In this instance the compiler takes the ternary line and creates an if statement... so it turns on to be the exact same thing.

Link: http://www.dotnetperls.com/ternary

Justin Self
  • 6,137
  • 3
  • 33
  • 48
0

You need as justnS said, ternary operators will be converted to if statements, while compiling, but if your using a ternary operator, you will need an if and an else part, while the if statement has no else. So maybe there will be a noticeable difference, if you run the code a few million or billion times. But it does not matter, if your building a normal program.

CriticalError
  • 145
  • 1
  • 1
  • 3