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开发者_JAVA技巧 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.
In general, you should worry about code readability and maintainability over and above this sort of performance micro-optimisation.
Unless this switch is inside a loop which is being executed 1000's (millions?) of times, this is highly unlikely to be the performance bottlebeck of your app.
Make a decision and stick with it for consistency's sake. In general, don't performance tune code until you have analysed where your performance bottlenecks are.
See also this question.
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.
精彩评论