C # 6 methods to determine the null character of the efficiency of the measured comparison _c# tutorial

Source: Internet
Author: User

C # Provides a wealth of methods or properties to determine whether a character is empty, and the common methods are the following 6

1. strtest== ""

2. Strtest.equals ("")

3. strtest== string. Empty

4. Strtest.equals (String. Empty)

5. Strtest.length = 0

6. String. IsNullOrEmpty (strtest)

In order to have an intuitive feeling about the efficiency of the above 6 methods, I specially wrote the following test code:

Using System;
    Namespace Strtest {class Program {//define 3 strings to test the speed of the following 6 methods of judging in a variety of situations public static string strTest01 = ""; public static string strTest02 = String.
    Empty;

    public static string strTest03 = "0123456789"; public static DateTime start, end;    Define start time and end time public static TimeSpan TS; Defines two-time intervals//********************** strtest using 6 test methods ***************************** public static void Test (string
      strtest) {//string = = "" Start = DateTime.Now;
      for (int counter = 0; counter <= 100000000; counter++) {if (strtest = "") {}}
      end = DateTime.Now;
      ts = End-start; Console.WriteLine ("String =/"/"Time consumed" + TS.)

      TotalSeconds + "seconds"); String.
      Equals ("") start = DateTime.Now;
      for (int counter = 0; counter <= 100000000; counter++) {if (Strtest.equals ("")) {}
      } end = DateTime.Now; ts = End-start;
      Console.WriteLine ("String. Equals (/"/") time consumption is "+ TS".

      TotalSeconds + "seconds"); String = = Stirng.
      Empty start = DateTime.Now; for (int counter = 0; counter <= 100000000; counter++) {if (strtest = string.
      Empty) {}} end = DateTime.Now;
      ts = End-start; Console.WriteLine ("String = = String. Empty time Consumption is "+ TS".

      TotalSeconds + "seconds"); String. Equals (String.
      Empty) start = DateTime.Now; for (int counter = 0; counter <= 100000000; counter++) {if Strtest.equals (string.
      Empty)) {}} end = DateTime.Now;
      ts = End-start; Console.WriteLine ("String. Equals (String. Empty) Time consumption is "+ TS".

      TotalSeconds + "seconds"); String.
      Length = = 0 start = DateTime.Now;
      for (int counter = 0; counter <= 100000000; counter++) {if (strtest.length = 0) {}
      } end = DateTime.Now;
     ts = End-start; Console.WriteLine ("String. Length = = 0 Time is consumed as "+ ts".

      TotalSeconds + "seconds"); String.
      IsNullOrEmpty (string) start = DateTime.Now; for (int counter = 0; counter <= 100000000; counter++) {if (string).
      IsNullOrEmpty (Strtest)) {}} end = DateTime.Now;
      ts = End-start; Console.WriteLine ("String. IsNullOrEmpty (string) time is consumed as "+ TS.
    TotalSeconds + "seconds" + "n");
      } static void Main (string[] args) {Console.WriteLine ("=======================================");     
      Console.WriteLine ("Strtest =/"/"5 kinds of test results");

      Console.WriteLine ("=======================================");

      Test (strTest01);
      Console.WriteLine ("======================================="); Console.WriteLine ("Strtest = string. 
      5 test results of emtpy ");

      Console.WriteLine ("=======================================");

      Test (strTest02);
      Console.WriteLine ("======================================="); Console.wrIteline ("strtest =/" 0123456789/"5 Kinds of test results");

      Console.WriteLine ("=======================================");

      Test (strTest03);  Console.ReadLine (); Wait for keyboard input: Pause the screen here}}

I turned off all the software I could turn off. Shielding the system as much as possible and allowing 6 methods to run 100 million times

Screenshot for the first time:

The second screenshot:

As you can see from the above: strings in three cases, string. The efficiency of Length = 0 is undoubtedly the highest.

Summarize

1. strtest== "" is not recommended, only to determine the "value is an empty string" string variable, and less efficient.

2. Strtest.equals ("") is not recommended for use, same as 1.

3. strtest== string. Empty is not recommended and can only be used to determine a string variable with a value of null and is inefficient.

4. Strtest.equals (String. Empty) is not recommended for use, with 3.

5. Strtest.length = 0 This way, I do not like to use, not recommended. In your actual test, you can prove that this is the most efficient way to do this, but to use it you must ensure that the string is not NULL, and if it is null, an exception is reported.

6. String. IsNullOrEmpty (Strtest) This method is my favorite, it not only one-time to judge "empty string variable", but also to determine the "value is an empty string of variables", but also to make the code simple and beautiful. The efficiency of judgment is not too low.

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.