问题描述
我想知道if是否比AND语句更好.我有一个循环运行了很多次,因此我正在考虑可以更快地执行.以下是与我的代码具有相同逻辑的代码.嵌套的if语句在循环内.
I am wondering whether nested if is better than AND statement. I have a loop that goes so many times so I am thinking of faster execution available. Below is the code that has same logic with my code. The nested if statement is inside a loop.
for ( int i = 0; i < array.length; i++)
{
// do stuff
if (x == 5)
{
if (y == 3)
{
// do stuff
}
}
}
如果我用this和STATEMENT替换嵌套的if,我的代码会更快吗?
Will my code be faster by a significant difference if I replace the nested if with this And STATEMENT?
if ((x == 5) && (y == 3))
// do stuff
我已经阅读了此链接,但没有找到答案.我是一名学生,仍在学习,感谢您的所有反馈!
I have read this link but I didn't find the answer. I am a student and still learning, thanks for all the feedback!
推荐答案
.NET将停止检查条件的第一部分是否为false,因此两者之间没有性能差异.
.NET will stop checking if the first part of the conditional is false, so there will be no performance difference between the two.
这篇关于用AND替换嵌套的if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!