这个简单的条件运算符会在编译时优化吗? (。网)
使用遗留代码,我发现我得到了很多这样的语句(超过 500 条),像这样
bool isAEqualsB = (a == b) ? true : false;
重写它有任何意义吗?
bool isAEqualsB = (a == b)
或者会在编译时进行优化?
预先感谢,
桑蒂! =)
Working with legacy code, I found I got are lot of statements (more than 500) like this
bool isAEqualsB = (a == b) ? true : false;
Does it make any sense to rewrite it like this ?
bool isAEqualsB = (a == b)
Or will be optimized at compile time ?
Thanks in advance,
Santi! =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
忽略性能 - 这所以不太可能成为瓶颈,除非您证明它与适当的基准相关,否则您不应该考虑它。
不过,我绝对会关心可读性 - 从这个角度来看,我认为第二种方法要好得多,并且肯定会使用它。
编辑:就优化而言,C# 编译器似乎没有对其进行优化:
但是,重要的不是 IL,而是 JIT 编译器所做的事情。现在,即使 IL 大小的差异也可能意味着内联和非内联之间的差异......
Ignore the performance - that's so unlikely to be a bottleneck, it shouldn't be what you think about until you've proved that it's relevant with appropriate benchmarks.
I would absolutely care about the readability though - and from that point of view, I consider the second approach to be much better, and would certainly use it.
EDIT: In terms of optimization, it looks like the C# compiler doesn't optimize it:
However, it's not the IL that will matter of course - it's what the JIT compiler does. Now even the difference in IL size may mean the difference between inlining and not...
无论是否优化,替换第一条语句都是有意义的。
这只是更干净的代码
a == b
是一个布尔表达式,不需要其他任何东西。将其视为提高代码库的可维护性和可读性的重构,而不一定是性能的提升。It makes sense to replace the first statement regardless of whether it will be optimized or not.
This is just cleaner code
a == b
is a boolean expression, no need for anything else. Think about it as a refactoring to improve maintainability and readability of your code base, not necessarily a performance win.肯定重写它。编译器可能会进行此优化,但这并不重要,因为如果您现在花时间修复它,代码将更具可读性。
更好的是,首先取消这些变量声明并用
a == b
替换它们的使用。另外,找到编写这段代码的人并给他们邪恶的眼睛:)
Definitely rewrite it. The compiler might make this optimization, but that's not important since the code will be much more readable if you put the time in to fix it now.
Better yet, do away with these variable declarations in the first place and replace their uses with
a == b
.Also, find whoever wrote this code and give them the evil eye :)
如果代码有效,我会保持原样。你花在重写它上的时间永远不会得到回报。是的,第二种方法更干净、更容易阅读,但是它会花费您时间来更改 500 多次出现的该模式,并且犯错误的机会(将工作代码变成非工作代码)是不可能的。零。从重构中获得的微小的可读性收益无法弥补进行更改所花费的时间和风险。
就执行而言,编译器几乎肯定会将第一个优化为第二个。但正如其他人所说,即使编译器没有对此进行优化,性能提升也微乎其微。
但我肯定会使用第二种方法来编写新代码。
If the code is working, I'd leave it the way it is. The time you'll spend rewriting it will never be repaid. Yes, the second approach is cleaner and easier to read, but it's going to take you time to change 500+ occurrences of that pattern, and the chance of making an error--turning working code into non-working code--is non-zero. The small readability benefit you'd get from the refactoring will not make up for the time and the risk involved with making the change.
As far as execution is concerned, the compiler will almost certainly optimize the first into the second. But as others have said, even if the compiler didn't optimize that, the performance gain would be miniscule.
But I would definitely use the second approach for new code.