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
Private void page_load (Object sender, system. eventargs E)
{
String A = "";
For (INT I = 0; I <= 1000000; I ++)
{
If (A = "")
{
}
}
}
Webform2.aspx
Private void page_load (Object sender, system. eventargs E)
{
String A = "";
For (INT I = 0; I <= 1000000; I ++)
{
If (A = string. Empty)
{
}
}
}
Webform3.aspx
Private void page_load (Object sender, system. eventargs E)
{
String A = "";
For (INT I = 0; I <= 1000000; I ++)
{
If (A. Length = 0)
{
}
}
}
InMicrosoft Application Center TestCreate three stress testing projects:
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 ).