gpt4 book ai didi

C# 执行速度 : equal (==) vs not equal (! =)

转载 作者:太空狗 更新时间:2023-10-29 23:54:15 27 4
gpt4 key购买 nike

<分区>

我正在尝试找出一些 if 语句的最佳实践。当我需要打开某种相等性时,即需要一个 if-else 结构时,我通常会在“不等于”上写条件。这背后的原因是,通常,当出现不成功的结果时,指令的数量及其复杂性都很低。比较过程有什么不同吗?等于(==)和不等于(!=)执行时间有区别吗?

示例(一个相当简单的示例,但总体思路成立):

string myString = "weekend";
if(myString != "weekend")
{
Console.WriteLine("No fun...!");
}
else
{
//do a bunch of fun stuff with relatively high complexity
//can expand over many lines of code
}

如果我改变 if-else 语句的顺序,执行时间会有什么不同吗?

string myString = "weekend";
if(myString == "weekend")
{
//do a bunch of fun stuff with relatively high complexity
//can expand over many lines of code
}
else
{
Console.WriteLine("No fun...!");
}

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com