Visual Basic 6 - "Select Case" VS "Elseif"

Which is better in terms of performance & memory usage, which is
higher performance and less memory usage?

"Select Case" VS "Elseif"

The language I am asking about is VB6.
rhinez0rzAsked:
Who is Participating?
 
List244Commented:
Well, memory is not really an issue here.  If you use an if or a select, you have all the memory already
spoken for.  As for performance, it seems they are the same.  It seems a select works just as an if, only
it looks little better when you start getting larger.

http://msdn2.microsoft.com/en-us/library/2h66e7a8.aspx

Seems to state that EACH case will be checked, like an if statement.  That is opposed to a language like C++
where you get greater performance using a switch (Select case) because it creates a jump table and is able
to skip much of the checking an if/else would not be able to.

So, I would suggest using whichever works best for you.
0
 
List244Commented:
One thing to note however, is the following:

A switch statement only needs to evaluate once, while if you have multiple if statments
asking the same thing, they have to evaluate more than once. For example:

Select Case 1 + 3
    Case 1:
        MsgBox "1"
    Case 2:
        MsgBox "2"
    Case 3:
        MsgBox "3"
    Case 4:
        MsgBox "4"
    Case Default:
        MsgBox "Default"
End Select
If 1 + 3 = 1 Then
    MsgBox "1"
ElseIf 1 + 3 = 2 Then
    MsgBox "2"
ElseIf 1 + 3 = 3 Then
    MsgBox "3"
ElseIf 1 + 3 = 4 Then
    MsgBox "4"
End If

The select case only had to evaluate 1+3 once while the if had to do it 4 times.
0
 
ShauliCommented:
Select Case will be preferred when ther are more than one action possible per condition, whereas ElseIf is per one action at a time. Example:

Select Case SomeCondition
    Case 2, 4, 6 To 9, 12
        'do your stuff
    Case 1, 3, 5
        'do the other stuff
    Case Else
        'take vacation
End Select

This you cannot do with ElseIf, so the answer is more driven by the number of expecetd results and less by memory usage and performace.

S
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
List244Commented:
Shauli, that is not correct, like I stated earlier, it is really about which is cleanest.  

If temp = 1 Or temp = 4 Or (temp >= 6 & temp <= 9) Or temp = 12 Then
    MsgBox temp
ElseIf temp = 1 Or temp = 3 Or temp = 5 Then
    MsgBox temp
Else
    MsgBox temp
End If

That is an equivalent if-else to your case statement.  It isn't quite as clean, but it works.  And like
I stated earlier, it has to check the value of temp much more than would be needed with a select
statement.
0
 
ShauliCommented:
That is correct. Of course your sample works, but it is not as efficient. As MSDN put it:

"...Tip   Select Case may be more useful when evaluating a single expression that has several possible actions...."

S
0
 
List244Commented:
Shauli, I have already pointed out that using the select case is more efficient in the way that it evaluates less.  I was
simply saying that your example which claimed the select case could not be done with an if-else, could.
0
 
ShauliCommented:
You can always travel from Philadelphia to New York through Timbuktu. Can it be done? Sure.

S
0
 
rhinez0rzAuthor Commented:
I meant to give credit for this answer:

Select Case will be preferred when ther are more than one action possible per condition, whereas ElseIf is per one action at a time. Example:

Select Case SomeCondition
    Case 2, 4, 6 To 9, 12
        'do your stuff
    Case 1, 3, 5
        'do the other stuff
    Case Else
        'take vacation
End Select

This you cannot do with ElseIf, so the answer is more driven by the number of expecetd results and less by memory usage and performace.

S


--------

Could you fix this? Thanks
0
 
List244Commented:
Rhinez0rz, that answer was incorrect.  That can be done with an if-else as I have showed you.
Also, if you read my previous comments, you will see that I already said that select would be
more efficient with multiple cases.  Only, I actually told you in addition to that, that it is because
of the fact that it must evaluate more times.  What was wrong with my answer?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.