An article about the performance analysis of the method of string null judgement, the experimental results have been given by the author, the conclusion is to use. Length==0 judgment is the most efficient, but the end of the article only a word, feeling not enough detail, so I write this article, calculate a supplement and study it.
Hardware environment for program code execution:
Cpu |
Intel T2300 1.66GHz |
Memory |
Kingston DDR2 667 1G |
Hard disk |
80G 5400 ext. 8m |
Software Environment for testing:
OS |
Windows XP Pro |
Ide |
VS 2008 RTM |
The code for the test is as follows:
3 variables are defined, 4 methods are called, and the test time is recorded for 100w Times:
stopwatch SW = new Stopwatch ()//Instantiate an object, recording time
string sEmpty1 = string. empty;//instantiates 3 string objects, and assigns the following values. Comparison test
for air separation
string sEmpty2 = "";
string sEmpty3 = "Stringnotempty";
////////////////////////////////////////////////////test sempty1///////////////////////////
SW. Start ()//Begin recording
for (int i = 0; I <= 1000000; i++)
{
if (sempty1.length = 0)
{ }
}
SW. Stop ()//stops recording time
Console.WriteLine ("String. Empty Length = = 0 Time cost is {0} ", SW. Elapsedmilliseconds);
////////
SW. Reset ()//Reset counter is 0;
SW. Start ();
for (int i = 0; I <= 1000000; i++)
{
if (sEmpty1 = "")
{ }
}
SW. Stop ();
Console.WriteLine ("String. Empty = \ "\" "Time cost is {0}", SW. Elapsedmilliseconds);
////
SW. Reset ();
SW. Start ();
for (int i = 0; I <= 1000000; i++)
{
if (sEmpty1 = = string. Empty)
{ }
}
SW. Stop ();
Console.WriteLine ("String. Empty = = String. Empty time cost is {0} ", SW. Elapsedmilliseconds);
SW. Reset ();
SW. Start ();
for (int i = 0; I <= 1000000; i++)
{
if (string. IsNullOrEmpty (sEmpty1))
{}
}
SW. Stop ();
Console.WriteLine ("String. IsNullOrEmpty time cost is {0} ", SW. Elapsedmilliseconds);
Console.WriteLine ();
////////////////////////////////////////////////////test sempty2///////////////////////////
SW. Reset ();
SW. Start ();
for (int i = 0; I <= 1000000; i++)
{
if (sempty2.length = 0)
{}
}
SW. Stop ();
Console.WriteLine ("\" \ "Length = = 0 Time") is {0} ", SW. Elapsedmilliseconds);
////////
SW. Reset ();
SW. Start ();
for (int i = 0; I <= 1000000; i++)
{
if (SEmpty2 = "")
{}
}
SW. Stop ();
Console.WriteLine ("\" \ = = \ "\" Time cost is {0} ", SW.) Elapsedmilliseconds);
/////
SW. Reset ();
SW. Start ();
for (int i = 0; I <= 1000000; i++)
{
if (SEmpty2 = = string. Empty)
{}
}
SW. Stop ();
Console.WriteLine ("\" = = string.) Empty Test3 time cost is {0} ", SW. Elapsedmilliseconds);
/////
SW. Reset ();
SW. Start ();
for (int i = 0; I <= 1000000; i++)
{
if (string. IsNullOrEmpty (sEmpty2))
{}
}
SW. Stop ();
Console.WriteLine ("\" \ "string.) IsNullOrEmpty time cost is {0} ", SW. Elapsedmilliseconds);
Console.WriteLine ();
////////////////////////////////////////////////////test sempty3///////////////////////////
SW. Reset ();
SW. Start ();
for (int i = 0; I <= 1000000; i++)
{
if (sempty3.length = 0)
{}
}
SW. Stop ();
Console.WriteLine ("\" stringnotempty\ "Length = = 0 Time cost is {0}", SW. Elapsedmilliseconds);
////////
SW. Reset ();
SW. Start ();
for (int i = 0; I <= 1000000; i++)
{
if (SEmpty3 = "")
{}
}
SW. Stop ();
Console.WriteLine ("\" stringnotempty\ "= = \" "Time cost is {0}", SW. Elapsedmilliseconds);
////
SW. Reset ();
SW. Start ();
for (int i = 0; I <= 1000000; i++)
{
if (sEmpty3 = = string. Empty)
{}
}
SW. Stop ();
Console.WriteLine ("\ stringnotempty\" = string. Empty Test3 time cost is {0} ", SW. Elapsedmilliseconds);
////
SW. Reset ();
SW. Start ();
for (int i = 0; I <= 1000000; i++)
{
if (string. IsNullOrEmpty (sEmpty3))
{}
}
SW. Stop ();
Console.WriteLine ("\" stringnotempty\ "IsNullOrEmpty time cost is {0}", SW. Elapsedmilliseconds);