Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleCompare the speeds of Choose and Select Case
KeywordsChoose, Select Case, performance
CategoriesTips and Tricks, Software Engineering
 
This example repeats a loop assigning a value using Choose and Select Case to see which is faster. It also creates an array of values and uses an index into the array to assign values.

In my tests, Choose took more than 10 times as long as Select Case. Select Case took about twice as long as the array assignment.

 
Private Sub btnGo_Click()
    Dim start_time As Single
    Dim stop_time As Single
    Dim num_trials As Long
    Dim i As Long
    Dim a As Long

    num_trials = CLng(txtNumTrials.Text)

    lblChoose.Caption = ""
    lblSelectCase.Caption = ""
    DoEvents

    start_time = Timer
    For i = 1 To num_trials
        a = Choose(1 + i Mod 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, _
            10)
    Next i
    stop_time = Timer
    lblChoose.Caption = Format$(stop_time - start_time, _
        "0.00") & " seconds"
    DoEvents

    start_time = Timer
    For i = 1 To num_trials
        Select Case 1 + i Mod 10
            Case 1
                a = 1
            Case 2
                a = 2
            Case 3
                a = 3
            Case 4
                a = 4
            Case 5
                a = 5
            Case 6
                a = 6
            Case 7
                a = 7
            Case 8
                a = 8
            Case 9
                a = 9
            Case 10
                a = 10
        End Select
    Next i
    stop_time = Timer
    lblSelectCase.Caption = Format$(stop_time - start_time, _
        "0.00") & " seconds"
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated