Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is faster - if..else or Select..case?

I have three condition to compare. Which one is more faster between the following two? Please point me out. Thanks all!

If var = 1 then
    Command for updating database
ElseIf var = 2 then
    Command for updating database
ElseIf var = 3 then
    Command for updating database
EndIf

and

Select Case var
   Case 1
      Command for updating database
   Case 2
      Command for updating database
   Case 3
      Command for updating database
End Select
like image 440
RedsDevils Avatar asked Nov 11 '09 03:11

RedsDevils


1 Answers

If you compile the two fragments and use reflector to disassemble you will see that they both end up as the practically the same IL. The compiler replaces the if / else with case statement.

This kind of micro optimization is highly unlikely to help you if you have performance problems.

If you have performance problems then you need to profile the program and find out where the bottlenecks are.

If you don't have performance problems, stop sweating this stuff and worry about writing code that is easily understood.

like image 50
Hamish Smith Avatar answered Oct 04 '22 00:10

Hamish Smith