|
-
Apr 2nd, 2000, 05:20 AM
#1
Thread Starter
Frenzied Member
About using IIF(...
Just so you know:
If performance is an issue at all for you, NEVER use Immediate If statements! They take at least 7.6 times as long to process as regular If Then statements.
If you don't believe me, try this...
Start a new project, paste this code into the form's code window, and run the project:
Code:
Option Explicit
Private Sub Form_Load()
Dim StartTime As Double 'Stores time at start of test
Dim EndTime As Double 'Stores time at end of test
Dim Result As Boolean 'Holds result of IIf
Dim nCounter As Long 'Counter to run test 1 million times
'Show form, tell user to wait
Me.Show
Print
Print " Please wait while running tests..."
Print
DoEvents
'********************'
' Immediate If Test: '
'********************'
'Get start time for IIf test
StartTime = CDbl(Timer)
'Run IIf 1 million times
For nCounter = 1 To 1000000
Result = IIf(True, True, False)
Next nCounter
'Get end time for IIf test
EndTime = CDbl(Timer)
'Print difference between start time and end time
Print " Time for IIf: " & EndTime - StartTime
Print
'********************'
' If/Then Test: '
'********************'
'Get start time for If/Then test
StartTime = CDbl(Timer)
'Run If/Then 1 million times
For nCounter = 1 To 1000000
If True Then
Result = True
Else
Result = False
End If
Next nCounter
'Get end time for If/Then test
EndTime = CDbl(Timer)
'Print difference between start time and end time
Print " Time for If/Then: " & EndTime - StartTime
End Sub
I just thought you guys might be interested...
~seaweed
-
Apr 2nd, 2000, 01:51 PM
#2
Addicted Member
It's quite true what seaweed says. In fact, IIf examines and executes both statements in the truepart and falsepart of the evaluation, while If executes only the relevant part. So quite obviously IIf takes almost twice as much time to execute that regular If
-
Apr 2nd, 2000, 01:55 PM
#3
Hyperactive Member
You guys waste a lot of code determing if a number is odd or even ;-)
Code:
Public Function IsEven(Number as Integer) As Boolean
IsEven = (Number mod 2 = 0)
End Function
Now you know it has to be one or the other so if it returns false you know what to do.
-
Apr 2nd, 2000, 04:55 PM
#4
transcendental analytic
OK, I knew everyone would give me that comment, but I just wanted to show how to use IIf. But in case of use with Msgbox, and other not so performance needing procedures, there's no practical difference. But I suppose if x mod 2 would be the best here.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|