|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Conditional statementsIs 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 ***
Show quote
Hide quote
"Niclas" <NOSpam@Notmail.com> schrieb It depends on what you're evaluating, thus there is no general answer. Run > 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 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. -- Show quoteHide quoteBest 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" <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 *** Niclas,
> Is there any performance differences between using Maybe but you have spoiled all that time by thinking about it and and write > > If->Then->Else->End if > > and > > Select Case -> case Else -> End Select ? > that message. (On a modern computer with even thousand of users is it so few that it is not worth thinking about it) Cor 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 *** 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 *** "Niclas" <NOSpam@Notmail.com> schrieb: I would not choose one over the other for matters of /performance/. > 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 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/> 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 -- Show quoteHide quoteHope this helps Jay [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "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 ***
date/time fields
throw/handle exceptions while using backgroundworker DUMB question about IndexOf Strange maximize behaviour Queue of Threads What is the correct case convention for Set? How to use a Generic's base type? timing accuracy of VB.Net checkedlistbox and mouseleave event deleting files greater than x days |
|||||||||||||||||||||||