locked
c# Performance Guide RRS feed

  • Question

  • Hi 

    Is there any document from Microsoft which guides you which option to use between the similar features in c#. Like sometimes Switch is better when compared to IF ELSE and Regex is better compared to using substring in getting sub texts from a string.

    Monday, September 18, 2017 4:53 AM

Answers

  • Hello Shishir Sasal,

    >>Is there any document from Microsoft which guides you which option to use between the similar features in c#.

    As far as I know, microsoft  didn't provide the such document for user to look up, but you could found some article about  performance in msdn document.You could take a look at the below link about performance.

    https://msdn.microsoft.com/en-us/library/ms973839.aspx

    And there is a good article that summarize the performance of common c# grammar . You could take reference with it.

    https://www.codeproject.com/Articles/10896/Effective-C-Performance-notes

    Best regards,

    feih_7


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Shishir Sasal Tuesday, September 19, 2017 3:45 PM
    Tuesday, September 19, 2017 7:33 AM

All replies

  • Many times it is dependent on the task at hand, one time a switch is best while another time if is best.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, September 18, 2017 12:55 PM
  • You need to evaluate the use cases because the examples you gave have very little to do with performance and a lot to do with the fact that they aren't interchangeable.

    An if-else can be used to test a series of conditions and execute code for each one. A switch statement is only useful when comparing a single expression (for equality) to a series of values that are known at compile time. If you don't meet the criteria for a switch then you have to use an if-else. 

    Same goes for a regex and substring. Regex can test for many patterns but it requires a compilation process first or inefficient string parsing. But substrings are limited to testing for strings within strings. If that is all you need to do then substring is faster. But if you're going to break a larger string apart then regex starts to become more efficient, especially if you will be doing it more than once.

    I would strongly recommend that you focus less on performance and more on proper usage of the language features available. The 80/20 rule applies to performance so trying to optimize an if-else to a switch is probably not going to gain you much if that code is executed only once. Focus on writing solid code and then run a profiler to identify the bottlenecks. Optimize those situations for the best performance increases.

    Michael Taylor
    http://www.michaeltaylorp3.net

    • Proposed as answer by Fei Hu Tuesday, September 19, 2017 7:04 AM
    Monday, September 18, 2017 1:53 PM
  • Hello Shishir Sasal,

    >>Is there any document from Microsoft which guides you which option to use between the similar features in c#.

    As far as I know, microsoft  didn't provide the such document for user to look up, but you could found some article about  performance in msdn document.You could take a look at the below link about performance.

    https://msdn.microsoft.com/en-us/library/ms973839.aspx

    And there is a good article that summarize the performance of common c# grammar . You could take reference with it.

    https://www.codeproject.com/Articles/10896/Effective-C-Performance-notes

    Best regards,

    feih_7


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Shishir Sasal Tuesday, September 19, 2017 3:45 PM
    Tuesday, September 19, 2017 7:33 AM