vb.net IIf() 和 If 之间的性能差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Performance difference between IIf() and If
提问by Bryan Roth
In Visual Basic, is there a performance difference when using the IIf
function instead of the If
statement?
在 Visual Basic 中,使用IIf
函数而不是If
语句时是否存在性能差异?
回答by Konrad Rudolph
VB has the following If
statement which the question refers to, I think:
VB有以下If
问题所指的陈述,我认为:
' Usage 1
Dim result = If(a > 5, "World", "Hello")
' Usage 2
Dim foo = If(result, "Alternative")
The first is basically C#'s ternary conditional operator and the second is its coalesce operator (return result
unless it's Nothing
, in which case return "Alternative"
). If
has thus replaced IIf
and the latter is obsolete.
第一个基本上是 C# 的三元条件运算符,第二个是它的合并运算符(返回,result
除非它是Nothing
,在这种情况下 return "Alternative"
)。If
因此已取代IIf
,后者已过时。
Like in C#, VB's conditional If
operator short-circuits, so you can now safely write the following, which is not possible using the IIf
function:
就像在 C# 中一样,VB 的条件If
运算符短路,因此您现在可以安全地编写以下内容,这是使用该IIf
函数无法实现的:
Dim len = If(text Is Nothing, 0, text.Length)
回答by Tom Mayfield
IIf()
runs both the true and false code. For simple things like numeric assignment, this isn't a big deal. But for code that requires any sort of processing, you're wasting cycles running the condition that doesn't match, and possibly causing side effects.
IIf()
运行真假代码。对于像数字赋值这样的简单事情,这没什么大不了的。但是对于需要任何类型处理的代码,您正在浪费运行不匹配条件的周期,并可能导致副作用。
Code illustration:
代码说明:
Module Module1
Sub Main()
Dim test As Boolean = False
Dim result As String = IIf(test, Foo(), Bar())
End Sub
Public Function Foo() As String
Console.WriteLine("Foo!")
Return "Foo"
End Function
Public Function Bar() As String
Console.WriteLine("Bar!")
Return "Bar"
End Function
End Module
Outputs:
输出:
Foo!
Bar!
回答by rjzii
Also, another big issue with the IIf is that it will actually call any functions that are in the arguments [1], so if you have a situation like the following:
此外,IIf 的另一个大问题是它实际上会调用参数 [1] 中的任何函数,因此如果您遇到以下情况:
string results = IIf(Not oraData.IsDBNull(ndx), oraData.GetString(ndx), string.Empty)
It will actually throw an exception, which is not how most people think the function works the first time that they see it. This can also lead to some very hard to fix bugs in an application as well.
它实际上会抛出一个异常,这不是大多数人第一次看到它时认为该函数的工作方式。这也可能导致应用程序中出现一些非常难以修复的错误。
[1] IIf Function - http://msdn.microsoft.com/en-us/library/27ydhh0d(VS.71).aspx
[1] IIf 函数 - http://msdn.microsoft.com/en-us/library/27ydhh0d(VS.71).aspx
回答by Larry
Better use If instead of IIf to use the type inference mechanism correctly (Option Infer On)
更好地使用 If 而不是 IIf 以正确使用类型推断机制(Option Infer On)
In this example, Keywords is recognized as a string when I use If :
在这个例子中,当我使用 If 时,关键字被识别为一个字符串:
Dim Keywords = If(String.IsNullOrEmpty(SelectedKeywords), "N/A", SelectedKeywords)
Otherwise, it is recognized as an Object :
否则,它被识别为一个 Object :
Dim Keywords = IIf(String.IsNullOrEmpty(SelectedKeywords), "N/A", SelectedKeywords)
回答by some guy
I believe that the main difference between If and IIf is:
我认为 If 和 IIf 之间的主要区别是:
If(test [boolean], statement1, statement2) it means that according to the test value either satement1 or statement2 will executed (just one statement will execute)
Dim obj = IIF(test [boolean] , statement1, statement2) it means that the both statements will execute but according to test value one of them will return a value to (obj).
if(test [boolean], statement1, statement2) 表示根据测试值执行satement1或statement2中的一个(只执行一个语句)
Dim obj = IIF(test [boolean] , statement1, statement2) 这意味着这两个语句都将执行,但根据测试值,其中一个将返回一个值 (obj)。
so if one of the statements will throw an exception it will throw it in (IIf) anyway but in (If) it will throw it just in case the condition will return its value.
因此,如果其中一个语句会抛出异常,它无论如何都会在 (IIf) 中抛出它,但在 (If) 中它会抛出它,以防条件返回其值。
回答by Greg Hurlman
回答by EdgarVerona
On top of that, readability should probably be more highly preferred than performance in this case. Even if IIF was more efficient, it's just plain less readable to the target audience (I assume if you're working in Visual Basic, you want other programmers to be able to read your code easily, which is VB's biggest boon... and which is lost with concepts like IIF in my opinion).
最重要的是,在这种情况下,可读性可能比性能更受欢迎。即使 IIF 更有效,它对目标受众来说也很不可读(我假设如果您使用 Visual Basic,您希望其他程序员能够轻松阅读您的代码,这是 VB 最大的好处......在我看来,IIF 之类的概念会丢失)。
Also, "IIF is a function, versus IF being part of the languages' syntax"... which implies to me that, indeed, If would be faster... if for nothing else than that the If statement can be boiled down directly to a small set of opcodes rather than having to go to another space in memory to perform the logic found in said function. It's a trite difference, perhaps, but worth noting.
此外,“IIF 是一个函数,而 IF 是语言语法的一部分”......这对我来说意味着,确实,If 会更快......如果没有别的,If 语句可以直接归结为到一小组操作码,而不必转到内存中的另一个空间来执行在所述函数中找到的逻辑。也许这是一个陈腐的差异,但值得注意。
回答by Dillie-O
...as to why it can take as long as 6x, quoth the wiki:
...至于为什么它可能需要长达 6 倍的时间,引用 wiki:
Because IIf is a library function, it will always require the overhead of a function call, whereas a conditional operator will more likely produce inline code.
因为 IIf 是一个库函数,它总是需要函数调用的开销,而条件运算符更可能产生内联代码。
Essentially IIf is the equivalent of a ternary operator in C++/C#, so it gives you some nice 1 line if/else type statements if you'd like it to. You can also give it a function to evaluate if you desire.
本质上,IIf 相当于 C++/C# 中的三元运算符,因此如果您愿意,它会为您提供一些不错的 1 行 if/else 类型语句。如果您愿意,您还可以给它一个函数来进行评估。
回答by titol
Those functions are different! Perhaps you only need to use IF statement. IIF will always be slower, because it will do both functions plus it will do standard IF statement.
这些功能不一样!也许你只需要使用 IF 语句。IIF 总是会比较慢,因为它会同时执行这两个功能,而且还会执行标准的 IF 语句。
If you are wondering why there is IIF function, maybe this will be explanation:
如果你想知道为什么有 IIF 函数,也许这将是解释:
Sub main()
counter = 0
bln = True
s = iif(bln, f1, f2)
End Sub
Function f1 As String
counter = counter + 1
Return "YES"
End Function
Function f2 As String
counter = counter + 1
Return "NO"
End Function
So the counter will be 2 after this, but s will be "YES" only. I know this counter stuff is useless, but sometimes there are functions that you will need both to run, doesn't matter if IF is true or false, and just assign value from one of them to your variable.
因此,此后计数器将为 2,但 s 仅为“是”。我知道这个计数器的东西没用,但有时你需要两个函数才能运行,IF 是真还是假都没有关系,只需将其中之一的值分配给你的变量。