问题描述
我一直在尝试优化我的代码,使其更加简洁和可读,并希望这样做不会导致性能下降.我认为我的更改可能会减慢我的应用程序,但它可能只是在我的脑海中.之间是否有任何性能差异:
I've been trying to optimize my code to make it a little more concise and readable and was hoping I wasn't causing poorer performance from doing it. I think my changes might have slowed down my application, but it might just be in my head. Is there any performance difference between:
Command.Parameters["@EMAIL"].Value = email ?? String.Empty;
和
Command.Parameters["@EMAIL"].Value = (email == null) ? String.Empty: email;
和
if (email == null)
{
Command.Parameters["@EMAIL"].Value = String.Empty
}
else
{
Command.Parameters["@EMAIL"].Value = email
}
我对可读性的偏好是空合并运算符,我只是不希望它影响性能.
My preference for readability would be the null coalescing operator, I just didn't want it to affect performance.
推荐答案
恕我直言,优化可读性和理解性 - 与您在现实世界中花费的时间相比,任何运行时性能提升都可能微乎其微几个月后回到这段代码,试着理解你最初在做什么.
IMHO, optimize for readability and understanding - any run-time performance gains will likely be minimal compared to the time it takes you in the real-world when you come back to this code in a couple months and try to understand what the heck you were doing in the first place.
这篇关于?:操作员与.If 语句性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!