dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: 'switch' statement efficiency in C#

  1. #1
    Join Date
    May 2007
    Posts
    9

    Question 'switch' statement efficiency in C#

    I am new to C#, and have come across a situation where I need to perform different tasks based on the criteria value (string).

    Thought of using 'switch' statement, I know (from C++), if you order the 'case' statements according to most frequenly occuring 'cases' at the top, it will optimise the 'switch' statement (don't know for sure, but I practiced it), but in C#, order of the 'cases' doesn’t matter.

    In my situation, I would end up with around 50 'cases', so I would like to know if there are any other alternatives (to improve efficiency) apart from 'switch' statement?

    Any thought/help would be greatly appreciated.

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: 'switch' statement efficiency in C#

    A (C++) switch statement is translated to comparison and jump operations in machine code. So the more cases the more jumps. Of course, if your match case is at the end, more comparison/jumps will be executed than if it was at the beginning. But I think this is a kind of detail that you should not care for. The days of 100MHz processors with 5 pipeline stages are gone. Better concentrate on the clarity of your code, whether you write in C++ or C#. Remember the rules of optimization:
    • don't optimize
    • don't optimize yet (for experts only)
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: 'switch' statement efficiency in C#

    Think about the desing. Maybe it could be redesigned using strategy pattern. If it would be possible, no switch and thus no dispatching will be neccessary.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,933

    Re: 'switch' statement efficiency in C#

    If you are interested in speed, you may not want to compare strings.

  5. #5
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: 'switch' statement efficiency in C#

    in .NET switch statements are more often than not implemented as hash tables, so placements doesn't matter like it does if it were doing the same thing as an if statement.

  6. #6
    Join Date
    May 2007
    Posts
    1,546

    Re: 'switch' statement efficiency in C#

    You can't fall-through on a C# switch statement. The result of this is that the order in which you write the statements doesn't matter.

    If you switch on strings, you'll end up using a hashtable. If you switch on an integer, then you'll end up with a jump table.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  7. #7
    Join Date
    Sep 2004
    Posts
    1,361

    Re: 'switch' statement efficiency in C#

    I would guess it is an O1 operation. Dictionaries can look up strings as O1, so switch statement's can't be any worse then that. In fact, O1 is as fast as you can get.

  8. #8
    Join Date
    Sep 2004
    Posts
    1,361

    Re: 'switch' statement efficiency in C#

    BTW just in case speed really is a big concern for you, as in you are writing a game, there are things to avoid like the plague:

    Setters and Getters are very expensive. Just make the members public.

    For J = 0 .... loops are faster then Foreach loops, but not by a lot.

    Use generic List<> instead of arrays. They are just as fast and can dynamically grow as needed. Internally they use arrays.

    Do intelligent memory allocation. For example, if you have a list of sprites that is 100 big and you need 1 more, do not add 1 more, but add 100 in one big chunk.

    There are a lot of other things to do for speed, but I do not want to go on and on about them. Maybe you are just idly curios and don't really care about maximum speed for everything.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)