Ternary ? operator vs the conventional If-else ope

2019-01-13 18:02发布

Possible Duplicate:
Is the conditional operator slow?

I'm a massive user of the ? operator in C#. However my project manager frequently warns me that using ? operator might cost some performance compared to the If-Else statements in a large scale application. So I'm told to avoid using it. However, I love using it because it is concise and sort of keeps the code clean.

Is there such performance overhead when using ? operator?

5条回答
放我归山
2楼-- · 2019-01-13 18:18

It is very hard to read ternary operations. If you use nested conditions, understanding ternary will become a overhead. Try to avoid ternary if there are more number of conditions.

查看更多
劳资没心,怎么记你
3楼-- · 2019-01-13 18:21

There's no reason to expect any difference in performance.

In my opinion, the ternary operator should only be used if all three operands are very concise and easy to read. Otherwise I think it has the potential to make code harder to read.

I think a lot of people mis-use this operator by jamming too much logic into one long line of code. I personally won't use it unless the whole line is less than about 80 characters.

Good:

return isFunky ? funkyValue : null;

Bad:

return (thisThing == thatThing && (anotherThing != null || ! IsThisTrue())) ? someThing.GetThis().GetThat() : yetAnotherThing.GetBlah().GetFoo();

I've seen people do a lot worse than the above. I think they should loose their ternary privileges!

查看更多
闹够了就滚
4楼-- · 2019-01-13 18:24

From my personal point of view, i don't see any performance differences between ternary operator and if statement.Many programming languages supports it and tenary operator is more developer friendly where as conventional If-else operator is understandable in general way.

http://en.wikipedia.org/wiki/%3F%3a

查看更多
啃猪蹄的小仙女
5楼-- · 2019-01-13 18:27

I ran 100 million Ternary Operators and 100 million If-Else statements and recorded the performance of each. Here is the code:

Stopwatch s = new Stopwatch();
// System.Diagnostics Stopwatch
int test = 0;
s.Start();
for(int a = 0; a < 100000000; a++)
    test = a % 50 == 0 ? 1 : 2;
s.Stop();

s.Restart();
for(int b = 0; b < 100000000; b++)
{
    if(b % 50 == 0)
        test = 1;
    else
        test = 2; 
}
s.Stop();

Here is the results (ran on an Intel Atom 1.66ghz with 1gb ram and I know, it sucks):

  • Ternary Operator: 5986 milliseconds or 0.00000005986 seconds per each operator.

  • If-Else: 5667 milliseconds or 0.00000005667 seconds per each statement.

Don't forget that I ran 100 million of them, and I don't think 0.00000000319 seconds difference between the two matters that much.

查看更多
神经病院院长
6楼-- · 2019-01-13 18:30

No.

Use what makes your code readable. If if statements do that, use them. If ternary operators do that, use them.

It is likely that both will compile down to the same IL anyway.

In any event the things that will slow down your application will likely be the database or the network or the hard drive ... anything except whether you used if statements or ternary expressions.

查看更多
登录 后发表回答