Let code refactoring fade-away series (3) -- string. Equals replace direct comparison and non-Comparison

Source: Internet
Author: User

Reconstruction background and causes

Recently, due to the continuous expansion of the project team, the Code style in the project varies, and the project may be full of flowers or even anger. Taking into account the survival and development of the team, after many battles, the project team was finally decided to be divided into several teams based on the business to strengthen team management and improve efficiency, while also cultivating tiered talents. For the sake of "Uniform" code style, each team improves the code capability of members so as to ultimately improve the quality of project code and reduce the maintenance cost in the future, the final decision is "Daily" for code lookup/review in the group, and then code refactoring.

 

Direct comparison and non-comparison: the so-called direct comparison and non-comparison refer to "=" Judgment Method and "! = "Judgment method, as shown in the following code

 1  public static void 直接判断() 2         { 3             Stopwatch timer = new Stopwatch(); 4             timer.Start(); 5  6             string str = "测试字符串"; 7             for (int i = 0; i < 10; i++) 8             { 9                 Stopwatch timer2 = new Stopwatch();10                 timer2.Start();11                 for (int j = 0; j < 10000000; j++)12                 {13                     if ("测试字符串" == str)14                     {15                     }16                 }17                 timer2.Stop();18                 Console.WriteLine("直接判断第" + i + "次耗时:" + (timer2.ElapsedMilliseconds));19 20             }21             timer.Stop();22             Console.WriteLine("直接判断平均耗时:" + (timer.ElapsedMilliseconds * 1.0 / 10));23 24         }25 26         public static void 非判断()27         {28             Stopwatch timer = new Stopwatch();29             timer.Start();30 31             string str = "测试字符串";32             for (int i = 0; i < 10; i++)33             {34                 Stopwatch timer2 = new Stopwatch();35                 timer2.Start();36                 for (int j = 0; j < 10000000; j++)37                 {38                     if ("测试字符串" != str)39                     {40 41 42                     }43                 }44                 timer2.Stop();45                 Console.WriteLine("非判断第" + i + "次耗时:" + (timer2.ElapsedMilliseconds));46 47             }48             timer.Stop();49             Console.WriteLine("非判断平均耗时:" + (timer.ElapsedMilliseconds * 1.0 / 10));50 51         }

When I see this, some friends will say that there is nothing to say, it has no impact, the effect is different? Let's take a look at the above code execution results. The execution results are as follows:

When I saw this picture, some friends may have laughed and said, the difference is also "too big". The difference between 10000000 and 2.6 milliseconds is only interesting? I'm glad to write it here. I just think that since there is a performance difference, why don't we need a better one?

Thank you for pointing out that string. Equals has better performance. After my test, the test results are as follows:

Time consumption: String. Equals <=<! =

Conclusion: String. Equals is preferred for string comparison.

 

Or, if we think about this when writing code, do we still need to spend more time doing This refactoring? How can code reconstruction not get farther and farther away?

 

Links to the series of articles are as follows:

Let code refactoring fade-away series (1) -- lift multi-layer nesting and let code refactoring fade-away series (2) -- reduce code duplication and let code refactoring fade-away series (3) -- direct comparison replaces non-Comparison

 

Let code refactoring fade-away series (3) -- string. Equals replace direct comparison and non-Comparison

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.