Conditional statements

  • Thread starter Thread starter Niclas
  • Start date Start date
N

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
 
Niclas said:
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
 
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
 
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
 
Niclas said:
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.
 
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
 
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
 
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


| 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
|
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top