三元 ? 运算符与 C# 中的传统 If-else 运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11643137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ternary ? operator vs the conventional If-else operator in c#
提问by Paras
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-Elsestatements 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.
我是?C# 运算符的大量用户。然而,我的项目经理经常警告我,?与If-Else大型应用程序中的语句相比,使用运算符可能会降低一些性能。所以我被告知避免使用它。但是,我喜欢使用它,因为它简洁并且可以保持代码干净。
Is there such performance overhead when using ?operator?
使用?运算符时是否有这样的性能开销?
采纳答案by matthewr
I ran 100 million Ternary Operators and 100 million If-Else statements and recorded the performance of each. Here is the code:
我运行了 1 亿条三元运算符和 1 亿条 If-Else 语句,并记录了每个语句的性能。这是代码:
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):
这是结果(在 Intel Atom 1.66ghz 和 1gb ram 上运行,我知道,它很糟糕):
Ternary Operator: 5986 milliseconds or 0.00000005986 seconds per each operator.
If-Else: 5667 milliseconds or 0.00000005667 seconds per each statement.
三元运算符:每个运算符 5986 毫秒或 0.00000005986 秒。
If-Else:每条语句 5667 毫秒或 0.00000005667 秒。
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.
别忘了我跑了 1 亿次,我不认为两者之间的 0.00000000319 秒差异那么重要。
回答by Anthony Mills
No.
不。
Use what makes your code readable. If ifstatements do that, use them. If ternary operators do that, use them.
使用使您的代码可读的内容。如果if语句这样做,请使用它们。如果三元运算符这样做,请使用它们。
It is likely that both will compile down to the same IL anyway.
无论如何,两者都可能编译为相同的 IL。
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 ifstatements or ternary expressions.
在任何情况下,会降低应用程序速度的因素很可能是数据库、网络或硬盘驱动器……除了您使用if语句或三元表达式之外的任何东西。
回答by mesimplybj
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.
从我个人的角度来看,我认为三元运算符和 if 语句之间没有任何性能差异。许多编程语言都支持它,并且三元运算符对开发人员更友好,而传统的 If-else 运算符通常可以理解。
回答by jahroy
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.
我认为很多人误用了这个运算符,将太多的逻辑塞进一长串的代码中。除非整行少于 80 个字符,否则我个人不会使用它。
Good:
好的:
return isFunky ? funkyValue : null;
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!
我见过人们做得比上面的差很多。我认为他们应该失去三元特权!
回答by jahroy
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.
阅读三元运算非常困难。如果使用嵌套条件,理解三元将成为开销。如果有更多条件,请尽量避免使用三元。

