VB6 IIf优势 [英] VB6 IIf advantage
问题描述
使用 IIf
比使用 If
有性能优势吗?
Is there a performance advantage to using IIf
over If
?
除了简单的更少的代码...还有什么区别:
Other than simply less code... what is the difference between :
If msInitialFloodSection <> Trim$(cboFloodSection.Text) Then
mbFloodSectionChanged = True
Else
mbFloodSectionChanged = False
End If
和
mbFloodSectionChanged = IIf(msInitialFloodSection <> Trim$(cboFloodSection.Text), True, False)
推荐答案
IIf
不是运算符或语言结构,它是一个函数,就像任何其他函数如 Left代码>.因此,它将始终评估其所有参数,而使用
If
您将只评估正确的分支.
IIf
is not an operator or a language construct, it's a function, just like any other function such as Left
. It will therefore evaluate all its arguments at all times, whereas with If
you will only evaluate the correct branch.
示例:
denominator = 0
value = IIf(denominator = 0, 0, value / denominator)
尽管存在分母为零的单独分支,这仍会引发除以零
错误.
This will raise Divizion by zero
error despite a separate branch exists for denominator being zero.
关于性能,它将做的是将您的值打包到 Variants 中,这将需要额外的滴答声,根本不需要那么多,但是如果我们在性能上,那么 If
会更快,因为它不会通过 Variants 强制执行,因为它只会计算其中一个值,而不是两个.
Regarding performance, what it will do is packing your values into Variants which will require additional ticks, not that much at all, but if we're on performance, then If
will be faster because it wouldn't coerce things through Variants and because it will only calculate one of the values, not two.
这篇关于VB6 IIf优势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!