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).