Asp.ent (C #) 3 ways to judge an empty string and performance analysis _ Practical Tips

Source: Internet
Author: User
The 3 ways are:
String A= "";
1.if (a== "")
2.if (A==string.empty)
3.if (a.length==0)

3 methods are equivalent, so which one is the highest performance? I use the experiment to explain the problem.

Create 3 aspx pages (why use Web pages, mainly with Microsoft application Center Test)

WebForm1.aspx
Copy Code code 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 Code code 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 Code code as follows:

private void Page_Load (object sender, System.EventArgs e)
{
String A= "";
for (int i=0;i<=1000000;i++)
{
if (a.length==0)
{
}
}
}

Set up 3 stress test projects under Microsoft Application Center test:

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

webform2.aspx-------if (a==string.empty)

webform3.aspx-------if (a.length==0)

So 3 ways to quantify the result is 98,105,168:

Method Results
if (a== "") 98
if (a==string.empty) 105
if (a.length==0) 168

So why is if (a.length==0) the fastest?
Because integer judgments are equal to the fastest, there is no complicated process, such as instantiation.

So: We recommend that you determine whether the string is empty 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.