C # five major performance optimization skills (up to 62 times)

Source: Internet
Author: User

5 tips for optimizing C # code performance 1. Have you used an exception mechanism to handle user input verification? If yes, your program performance has been reduced by 62 times. Don't you believe it? Wait a few minutes and I will tell you what's going on. However, before the example, it is really necessary to understand where exceptions occur. For example, if you verify the data entered by the user, an exception is thrown to the client (I assume you verify the user input based on the business logic ). [Csharp] class BusinessLogcCheck {public void Check () {try {// Your validation code is here} catch (Exception ex) {throw new Exception ("My own exception") ;}} dear friend, in the following example, when you see the screen output result, you will realize what a bad habit it is. Let's take a look at the following code. [Csharp] using System; using System. collections. generic; using System. linq; using System. text; using System. diagnostics; using System. IO; using System. net; using System. net. networkInformation; namespace Test1 {class Program {public static void ThrowTest () {throw new Exception ("This is required topn");} public static Boolean Return () {return false ;} static void Main (string [] args) {Stopwatch sw = New Stopwatch (); sw. start (); try {ThrowTest ();} catch {} sw. stop (); Console. writeLine ("With Exception" + sw. elapsedTicks); sw. restart (); try {Return ();} catch {} sw. stop (); Console. writeLine ("With Return" + sw. elapsedTicks); Console. readLine () ;}} this is the input result you are waiting. My proof is very simple. An exception is thrown in a function, and a Boolean value after user input validation is returned in another function. In addition, I added a calculator to convince you how exception handling affects code performance. Therefore, we can get a result: Do not throw an exception for user input validation, but use a Boolean value to return the business logic (or other similar technologies) of the validation input ). Because the cost of the exception object is too high. (It's not as high as your favorite shirt. Haha) 2. Never use try-Catch in a loop. Yes, this is also related to exception handling. I will repeat it again: Do not use try-Catch in a loop. Let me use an example to prove it. [Csharp] using System; using System. collections. generic; using System. linq; using System. text; using System. diagnostics; using System. IO; using System. net; using System. net. networkInformation; namespace Test1 {class Program {static void Method1 () {for (int I = 0; I <1000; I ++) {try {int value = I * 100; if (value =-1) {throw new Exception () ;}} catch {}} static void Method2 () {try {for (int I = 0; I <1000; I ++) {int value = I * 100; if (value =-1) {throw new Exception ();}}} catch {}} static void Main (string [] args) {Stopwatch sw = new Stopwatch (); sw. start (); Method1 (); sw. stop (); Console. writeLine ("Within Loop" + sw. elapsedTicks); sw. restart (); Method2 (); sw. stop (); Console. writeLine ("Outside of Loop" + sw. elapsedTicks); Console. readLine ();}}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.