. NET Deep Learning Notes (2): Performance comparison and analysis of 4 methods for judging empty strings in C #

Source: Internet
Author: User
Tags comparison empty reset

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.