C # performance analysis of the three methods for determining null strings,
3Methods:
String a = "";
1. if (a = "")
2. if (a = String. Empty)
3. if (a. Length = 0)
3Methods are equivalent,Which method has the highest performance? I will illustrate the problem with an experiment.
Create3ItemsAspxPageMicrosoft 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.aspxPrivate 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 Test Create3Stress testing projects:
Test results:
WebForm1.aspx ---------- if (a = "")
WebForm2.aspx ------- if (a = String. Empty)
WebForm3.aspx ------- if (a. Length = 0)
So3The result of quantification is98,105,168:
Method |
Result |
If (a = "") |
98 |
If (a = String. Empty) |
105 |
If (a. Length = 0) |
168 |
So why?If (a. Length = 0)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).
Symbol in C Language <Yes
Left shift operator (<)
Removes all the binary bits of an operation object from the left and adds 0 to the right ).
For example, a = a <2 shifts the binary bits of a two places to the left and complements 0 to the right,
Move 1 to the left and then a = a * 2;
If the left shift does not include 1 in the Discard high position, then shifts one bit left, which is equivalent to multiplying the number by 2.
Shift right operator (>)
Shifts all the binary bits of a number to several places to the right, and adds 0 to the left of the positive number, 1 to the left of the negative number, and discards the right of the negative number.
The operand shifts one digit to the right, which is equivalent to dividing the number by 2.
For example, a = a> 2 shifts the binary bit of a two places to the right,
0 or 1 to see whether the number is positive or negative.
Symbol in C Language <Yes
Left shift operator (<)
Removes all the binary bits of an operation object from the left and adds 0 to the right ).
For example, a = a <2 shifts the binary bits of a two places to the left and complements 0 to the right,
Move 1 to the left and then a = a * 2;
If the left shift does not include 1 in the Discard high position, then shifts one bit left, which is equivalent to multiplying the number by 2.
Shift right operator (>)
Shifts all the binary bits of a number to several places to the right, and adds 0 to the left of the positive number, 1 to the left of the negative number, and discards the right of the negative number.
The operand shifts one digit to the right, which is equivalent to dividing the number by 2.
For example, a = a> 2 shifts the binary bit of a two places to the right,
0 or 1 to see whether the number is positive or negative.