Search code examples
vb.netperformanceif-statementcase-statement

ElseIf or Case Statement in VB.NET


Are there any general performance guidlines for when to use a Case instead of a chain of ElseIf?

Please consider the following example...

        Dim lobjExample As Object = GetARandomDataTypeCrapExample()
        If lobjExample Is GetType(Boolean) Then
            ' Solve world hunger here 
        ElseIf lobjExample Is GetType(Byte) Then
            ' Solve world hunger here 
        ElseIf lobjExample Is GetType(Char) Then
            ' Solve world hunger here 
        ElseIf lobjExample Is GetType(Date) Then
            ' Solve world hunger here 
        ElseIf lobjExample Is GetType(Decimal) Then
            ' Solve world hunger here 
        ElseIf lobjExample Is GetType(String) Then
            ' Solve world hunger here 
        ElseIf lobjExample Is GetType(Integer) Then
            ' Solve world hunger here 
        ElseIf lobjExample Is GetType(Double) Then
            ' Solve world hunger here 
        ElseIf lobjExample Is GetType(Long) Then
            ' Solve world hunger here 
        ElseIf lobjExample Is GetType(SByte) Then
            ' Solve world hunger here 
        ElseIf lobjExample Is GetType(Short) Then
            ' Solve world hunger here 
        ElseIf lobjExample Is GetType(Single) Then
            ' Solve world hunger here 
        ElseIf lobjExample Is GetType(ULong) Then
            ' Solve world hunger here 
        ElseIf lobjExample Is GetType(UInteger) Then
            ' Solve world hunger here 
        ElseIf lobjExample Is GetType(UShort) Then
            ' Solve world hunger here 
        Else
            ' Cannot solve world hunger 
        End If

Would this be better suited as a case statement?


Solution

  • The VB.NET Select Case statement is very flexible. Much more so than the equivalent C# switch keyword. This is all rather intentional and part of the philosophy behind the languages. Where VB.NET is a friendly to the programmer, C# is friendly to the machine.

    The .NET Intermediate Language has a dedicated opcode to implement these kind of statements, Opcodes.Switch. It will be jitted to a jump table at runtime, very fast with no if-then-else code if the jump table is "perfect" with all slots filled.

    But it has a practical restriction, the jump table entries have to be know at compile-time. Which works fine if your Case statements are constant expressions. The kind that the C# compiler allows in the switch statement cases. But that cannot be the value of Object.GetType(), the "type handle" is generated at runtime and will have a value that entirely depends on what has been jitted before.

    So the VB.NET compiler does the programmer-friendly thing and translates your Select Case statement to a chain of If-Then-Else statements. Exactly like the kind you have written by hand. Use the Ildasm.exe tool to see this for yourself.

    So don't cramp your style, it doesn't make a difference. The only thing you'd really want to find out if a lookup table could make it faster. A Dictionary(Of Type, Integer), now your Select Case statement will be very fast since it can use the optimized version. But with the added cost of the Dictionary. Only you care enough about it to check that. Using real data is very important, synthetic tests tend to not properly test the number of If-Then tests that are needed. The order the Case statements matter a great deal, the most likely match should be on top.

    Perhaps notable is the upcoming Select Case TypeOf statement. It is just syntax sugar, not perf.