473,468 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Conditional statements

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 ***
Jan 25 '06 #1
7 1525
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" <NO****@Notmail.com> escribió en el mensaje
news:e5**************@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 ***

Jan 25 '06 #2
"Niclas" <NO****@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

Jan 25 '06 #3
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
Jan 25 '06 #4
"Niclas" <NO****@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/>

Jan 25 '06 #5

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 ***
Jan 25 '06 #6
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" <NO****@Notmail.com> wrote in message
news:ei**************@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 ***

Jan 25 '06 #7
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/r...ymorphism.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
"Niclas" <NO****@Notmail.com> wrote in message
news:e5**************@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 ***
Jan 25 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Max | last post by:
Hi, I'm a perl programmer and am trying to learn PHP. So far I have figured out most of the differences, but have not been able to find out how to do the following: When running through a...
8
by: neblackcat | last post by:
Would anyone like to comment on the following idea? I was just going to offer it as a new PEP until it was suggested that I post it here for comment & consideration against PEP 308. I'm far...
10
by: clueless_google | last post by:
hello. i've been beating my head against a wall over this for too long. setting the variables 'z' or 'y' to differing numbers, the following 'if/else' code snippet works fine; however, the ...
3
by: Jouke Langhout | last post by:
Hello all! For quite some time now, I've got the following problem: Access won't close properly when a user closes the application. An ACCESS process stays active and that process can only be...
1
by: chris han | last post by:
Hi, all, I'm trying to use Conditional Compilation Statements in my code. using System; #define DEBUG public class MyClass { public static void Main() {
92
by: Raghavendra R A V, CSS India | last post by:
hie.. Do any one knows how to write a C program without using the conditional statements if, for, while, do, switch, goto and even condotional statements ? It would be a great help for me if...
10
by: nimmi_srivastav | last post by:
Below you will see an example of a nested conditional expression that this colleague of mine loves. He claims that it is more efficient that a multi-level if-else-if structure. Moreover, our...
10
by: Dave | last post by:
I'm a C++ programmer of many years, trying to get my feet wet in C#. I have a question about conditional compilation. In C++, I would sometimes define a constant in an include file, and then...
5
by: Gary Wessle | last post by:
Hi I have a group of functions which have the same signature. void fun_n(void); according to a conditional structure "be it if-else or switch-case" I get to choose which one to run. ...
43
by: dev_cool | last post by:
Hello friends, I'm a beginner in C programming. One of my friends asked me to write a program in C.The purpose of the program is print 1 to n without any conditional statement, loop or jump. ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.