Results 1 to 4 of 4

Thread: how to detremen if x is even or a odd number ??

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357

    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


  2. #2
    Addicted Member pardede's Avatar
    Join Date
    Jan 2000
    Posts
    232
    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

  3. #3
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    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.

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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
  •  



Click Here to Expand Forum to Full Width