?:操作员比。如果声明性能操作员、声明、性能

2023-09-02 23:49:06 作者:你最可爱了

我一直在尝试优化我的code,使其多了几分简洁和可读性,并希望自己没有从这样做造成性能较差。我觉得我的变化可能已经放慢我的应用程序,但它可能只是在我的脑海。有没有之间的性能差异:

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
}

我的preference为便于阅读,将是空合并运算符,我只是不希望它影响性能。

My preference for readability would be the null coalescing operator, I just didn't want it to affect performance.

推荐答案

恕我直言,优化可读性和可理解性 - 任何运行时相比,它需要你在现实世界中,当你来到的时候性能提升可能是最小的回到这个code在一两个月,并尝试了解你到底你在做摆在首位。

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.

 
精彩推荐
图片推荐