Home All Groups Group Topic Archive Search About

Conditional statements

Author
25 Jan 2006 1:26 PM
Niclas
Hi,

Is there any performance differences between using

If->Then->Else->End if

and

Select Case -> case Else -> End Select ?

Understand that select case appears better when evaluating multiple
values, but would like to understand if one is better than the other for
example when evaluating 2 possible values or if they are equal

(Sad question, i know...)

Niclas



*** Sent via Developersdex http://www.developersdex.com ***

Author
25 Jan 2006 1:45 PM
Armin Zingler
Show quote Hide quote
"Niclas" <NOSpam@Notmail.com> schrieb
> Hi,
>
> Is there any performance differences between using
>
> If->Then->Else->End if
>
> and
>
> Select Case -> case Else -> End Select ?
>
> Understand that select case appears better when evaluating multiple
> values, but would like to understand if one is better than the other
> for example when evaluating 2 possible values or if they are equal


It depends on what you're evaluating, thus there is no general answer. Run
it in a loop for several seconds to test the performance in your specific
case.

Armin
Author
25 Jan 2006 1:45 PM
Carlos J. Quintero [VB MVP]
Hi Niclas,

I don´t know the exact answer, for that you would need to decompile and
compare the generated IL code (use ildasm.exe or Reflector for .NET), but my
guess is that the compiler should be clever enough to generate the same
code. So, just ensure that you put the most likely cases at the beginning.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com


Show quoteHide quote
"Niclas" <NOSpam@Notmail.com> escribió en el mensaje
news:e5w2jLbIGHA.1388@TK2MSFTNGP11.phx.gbl...
> Hi,
>
> Is there any performance differences between using
>
> If->Then->Else->End if
>
> and
>
> Select Case -> case Else -> End Select ?
>
> Understand that select case appears better when evaluating multiple
> values, but would like to understand if one is better than the other for
> example when evaluating 2 possible values or if they are equal
>
> (Sad question, i know...)
>
> Niclas
>
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
Author
25 Jan 2006 2:18 PM
Cor Ligthert [MVP]
Niclas,

> Is there any performance differences between using
>
> If->Then->Else->End if
>
> and
>
> Select Case -> case Else -> End Select ?
>
Maybe but you have spoiled all that time by thinking about it and and write
that message.

(On a modern computer with even thousand of users is it so few that it is
not worth thinking about it)

Cor
Author
25 Jan 2006 2:32 PM
Niclas
thanks for the reply, I was actually more after best coding practice,
and if there was a view if one was better than the other.

Niclas


*** Sent via Developersdex http://www.developersdex.com ***
Author
25 Jan 2006 3:32 PM
Marina
I think from a code reading perspective, if there is only one 'else', then
it makes more sense to use if/then/else.

If there are more then 2 possible cases, then a case statement is more
appropriate. I hate seeing
If .. Then
ElseIf... Then
ElseIf... Then.
End If

Show quoteHide quote
"Niclas" <NOSpam@Notmail.com> wrote in message
news:ei3WewbIGHA.3944@tk2msftngp13.phx.gbl...
>
> thanks for the reply, I was actually more after best coding practice,
> and if there was a view if one was better than the other.
>
> Niclas
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
Author
25 Jan 2006 2:30 PM
Herfried K. Wagner [MVP]
"Niclas" <NOSpam@Notmail.com> schrieb:
> Is there any performance differences between using
>
> If->Then->Else->End if
>
> and
>
> Select Case -> case Else -> End Select ?
>
> Understand that select case appears better when evaluating multiple
> values, but would like to understand if one is better than the other for
> example when evaluating 2 possible values or if they are equal

I would not choose one over the other for matters of /performance/.
Instead, choose the semantically more correct construct.  When checking if
two values are equal, 'If' is the natural choice.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
25 Jan 2006 5:50 PM
Jay B. Harlow [MVP - Outlook]
Niclas,
As the others suggest do not use "performance" to decide which to use, I
generally use which is "appropriate" to use.

For simply If/Else I will use an If statement.

    If someThing > someThingElse Then
        ...
    Else
        ...
    End If

If I have a "1 of  N" conditions (such as evaluating an Enum field) I will
use a Select Case.

    Select Case someThing
        Case "A"
            ...
        Case "B"
            ...
        Case "C"
            ...
        Case Else
            Throw ... something unexpected happened.
    End Select

About the only time I use ElseIf is when I an checking object types.

    If TypeOf obj Is ListView Then
    ElseIf TypeOf obj Is TreeView Then
    ElseIf TypeOf obj Is Button Then
    Else
        Throw ... something unexpected happened.
    End If

Yes, this means I favor (when coding) If/ElseIf over Select True/Case/Case.
However! I do see some value in Select True/Case/Case & would recommend it,
for those that follow it. Other languages I've used support it, however
currently C# & C++...

Of course ElseIf & Select Case should be examined for "Replace Conditional
with Polymorphism" refactoring possibilities:
http://www.refactoring.com/catalog/replaceConditionalWithPolymorphism.html


NOTE: I do not buy into the "Use Select All The Time" school of thought, I
definately don't do:

    Select True
        Case someThing > someThingElse
            ...
        Case Else
            ...
    End Select

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Show quoteHide quote
"Niclas" <NOSpam@Notmail.com> wrote in message
news:e5w2jLbIGHA.1388@TK2MSFTNGP11.phx.gbl...
| Hi,
|
| Is there any performance differences between using
|
| If->Then->Else->End if
|
| and
|
| Select Case -> case Else -> End Select ?
|
| Understand that select case appears better when evaluating multiple
| values, but would like to understand if one is better than the other for
| example when evaluating 2 possible values or if they are equal
|
| (Sad question, i know...)
|
| Niclas
|
|
|
| *** Sent via Developersdex http://www.developersdex.com ***