?:操作员比。如果声明性能 [英] ?: Operator Vs. If Statement Performance

查看:172
本文介绍了?:操作员比。如果声明性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试优化我的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.

这篇关于?:操作员比。如果声明性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆