Tests performed by beautyispower's post-comparison stringbuilder string

Source: Internet
Author: User
A small test,
According to beautyispower's original Code : Using System;
Using System. text;

Namespace Test
{
Public   Class Testapp
{
Public   Static   Void Main ( String [] ARGs)
{
Int Inttimeused = 0 ;
Int Intcount = 0 ;
Int Intmaxcount = 50000 ;
String Strtest = String . Empty;
Stringbuilder sbtest = New Stringbuilder (intmaxcount );


Console. writeline ( " Adding strings using stringbuilder. " );
Inttimeused = Environment. tickcount;
While (Intcount < Intmaxcount)
{
Intcount++;
Sbtest. append ("A");
}
Inttimeused = Environment. tickcount - Inttimeused;
Console. writeline ( " End with stringbuilder to add strings. " );
Console. writeline ( " Time: {0} milliseconds " , Inttimeused );

Intcount = 0 ;
Console. writeline ( " Start to use string to add strings. " );
Inttimeused = Environment. tickcount;
While (Intcount < Intmaxcount)
{
Intcount++;
Strtest+ ="A";
}
Inttimeused = Environment. tickcount - Inttimeused;
Console. writeline ( " End with string for string addition. " );
Console. writeline ( " Time: {0} milliseconds " , Inttimeused );


}
}
}

Stringbuilder is 0 ms, and string is about 5500 Ms
Change strtest + = "A"; in the original code to strtest = string. Intern (strtest) + "";
Stringbuilder seems to be slowing down. Sometimes it is 0 ms, sometimes it is 15-16 ms,
The string is faster and stable at around 2260.

I am not very clear about the reasons. Please analyze and guide me.

Note: This article references the code of beautyispower in stringbuilder string comparison.

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.