问题
Suppose there is a switch on some method like
static string switchExample(string abc){
switch(abc.ToUpper()) {
case "123":
return "Numeric";
case "ab":
return "Alphabets";
default:
return "symbol";
}
}
Now here I need to know if there are any performance issues to use multiple points of exit(return on each case) like I did in given code rather to pass updated value at one time by making some temp string at the start and filling it with corresponding case match.
回答1:
Now here i need to no is there any performance issue to use multiple points of exit
Your question is the epitome of premature optimization.
My suggestion, if you really want to know, is to write the method out both ways - once with return
statements in each case and once with a single return after the switch
statement. Then decompile each to intermediate language for comparison.
You still won't be able to make any certain statement about performance without doing actual measurement by profiling.
But, I think it's quite safe to say that any difference, if any, will be extremely miniscule. The end result should be nothing more than comparing the value to the constant strings, and when matched pushing the value and the stack and returning. The compiler will likely optimize away the assignment to a temporary variable, seeing that it will only be used as a return value.
Furthermore, some switch statements are significantly optimized by the compiler. See https://stackoverflow.com/a/395965/224087 and https://stackoverflow.com/a/126507/224087 for more information of switch statement performance and optimization.
This code you are questioning the performance of will only be a handful or two of CPU operations - a truly tiny amount of time to be concerned with.
回答2:
Testing with the Visual Studio 11 Developer Preview reveals that if your switch statement has fewer than 7 cases (6 cases and a default), then the compiler generates a series of if
statements. Your example code is converted to the equivalent of:
string s = abc.ToLower();
if (s == "123") return "Numeric";
if (s == "ab") return "Alphabetic";
return "symbol";
If there are 7 or more cases, the compiler generates code that creates a dictionary, mapping strings to sequential integers. It then uses an IL switch
statement (which similar to a computed goto in other languages) to branch to the different code based on the values.
The code is generated such that the dictionary is only created once--the first time the switch statement is executed. Subsequent uses of that switch statement don't have to re-create the dictionary.
In terms of efficiency, the compiler's code generated for a small switch statement is equivalent to multiple if statements. When there are more than a 6 cases (including the default), the compiler's dictionary lookup will be faster than the multiple switch statements.
Note that my numbers are based on a very small sample (a few tests) using the developer preview of an unreleased version of the compiler. Previous versions of the compiler might have a different threshold or might use other methods, and the final release version of the new compiler might do things differently than what I've described above. I wouldn't count on any of these implementation details.
In short, write the switch
. Let the compiler worry about how to make it faster.
来源:https://stackoverflow.com/questions/8529899/need-to-know-switch-performance-c-sharp