Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
580 views
in Technique[技术] by (71.8m points)

if statement - VB6 IIf advantage

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

and

mbFloodSectionChanged = IIf(msInitialFloodSection <> Trim$(cboFloodSection.Text), True, False)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

Example:

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...