4

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.

RollerCosta
  • 5,020
  • 9
  • 53
  • 71
  • In my opinion, the sooner you can return, and the least amount of variables/memory you use, the better / faster. – Travis J Dec 16 '11 at 04:50
  • 3
    I think the bigger performance hit is going to come from switching on a string. If you're that concerned about performance (where every instruction counts), switch to an int or an enum. – Chris Eberle Dec 16 '11 at 04:53
  • Travis J You mean what i did in my code cause to happen things faster – RollerCosta Dec 16 '11 at 04:54
  • 4
    There is no performance problem with multiple exit points. And switching on a string shouldn't be much slower than switching on an int. It's likely the compiler will create a dictionary for the lookup, especially if the number of cases is more than a handful. If it's easy to use an int or enum rather than a string, then of course go for it. But if doing so requires that you convert an input string to a number (or enum), then you've lost all benefit you might have gained. – Jim Mischel Dec 16 '11 at 05:03
  • Jim let me know one thing more" what should i pick b/w if...else if and switch when it comes to string???" – RollerCosta Dec 16 '11 at 05:07

2 Answers2

4

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.

Community
  • 1
  • 1
quentin-starin
  • 26,121
  • 7
  • 68
  • 86
  • +1. However, check your next-to-last sentence. I think it should say, "switch statements with *more than* a small number of cases." – Jim Mischel Dec 16 '11 at 05:33
4

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.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351