vb.netselectlogiccompiler-optimization

VB.NET Select Case Compiler Optimizations?


Does the VB.NET 2008 compiler selectively optimize Select Case Statements?

For example, a Select Case Statement with a sufficient quantity of integer cases could be organized as a binary search.

I ask this because I am curious whether or not I should opt for a Select Case in place of If Statements with multiple Else If's where integers or other basic data types are being compared.


Solution

  • Select Case with 40 choices is more than 10x faster than a string of 40 ElseIf statements. That is more improvement than you would expect to get with a binary search. I would guess that a simple integer Select Case uses whatever the modern machine code equivalent of a computed goto statement is -- it compiles so that it branches directly to the proper "case" based on the value of the integer.

    I think Select Case is the one to go with.