3 methods for determining null strings in asp. ent (C #) and Performance Analysis

Source: Internet
Author: User

The three methods are as follows:
String a = "";
1. if (a = "")
2. if (a = String. Empty)
3. if (a. Length = 0)

The three methods are equivalent, so which method has the highest performance? I will illustrate the problem with an experiment.

Create three aspx pages (why use web pages, mainly using Microsoft Application Center Test)

WebForm1.aspx
Copy codeThe Code is as follows:
Private void Page_Load (object sender, System. EventArgs e)
{
String a = "";
For (int I = 0; I <= 1000000; I ++)
{
If (a = "")
{
}
}
}

WebForm2.aspx
Copy codeThe Code is as follows:
Private void Page_Load (object sender, System. EventArgs e)
{
String a = "";
For (int I = 0; I <= 1000000; I ++)
{
If (a = String. Empty)
{

}
}
}

WebForm3.aspx
Copy codeThe Code is as follows:
Private void Page_Load (object sender, System. EventArgs e)
{
String a = "";
For (int I = 0; I <= 1000000; I ++)
{
If (a. Length = 0)
{
}
}
}

Create three stress testing projects under Microsoft Application Center Test:

Test results:
WebForm1.aspx ---------- if (a = "")

WebForm2.aspx ------- if (a = String. Empty)

WebForm3.aspx ------- if (a. Length = 0)

Therefore, the result of quantification in the three methods is 98,105,168:

Method Result
If (a = "") 98
If (a = String. Empty) 105
If (a. Length = 0) 168

So why?If (a. Length = 0) is the fastestWhat about it?
Because integer judgment is the fastest, there is no complicated process such as instantiation.

Therefore, it is recommended that you determine whether the string is null.If (a. Length = 0 ).

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.