Performance analysis of three methods for determining null strings in C #
Author: qingyueer
Home page:Http://blog.csdn.net/21aspnet/Time: 2007.4.28
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,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)
{
}
}
}
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 is if (A. Length = 0) the fastest?
Because integer judgment is the fastest, there is no complicated process such as instantiation.
Therefore, we recommend that you use if (A. Length = 0) to determine whether the string is null ).