Efficiency Testing for replacement (MS vs my)

Source: Internet
Author: User

Because the company wants to provide data to other companies, there are characters to replace in the data. There are two replacement methods
 
Code1:

Public   Static   Void Main ()
{

StringInput1= "";


Stringbuilder SB1 =   New Stringbuilder ();
For ( Int I = 0 ; I <   50000 ; I ++ )
{
Sb1.append ( " Wo \ r " );
}

Input1 = Sb1.tostring ();

WL ( " SB test date is complate .. " );

Stopwatch SW1 =   New Stopwatch ();


// Char
Sw1.start ();
Stringbuilder sb =   New Stringbuilder ();
If (Input1 ! = String. Empty)
{
For ( Int I =   0 ; I < Input1.length; I ++ )
{
Switch (Input1 [I])
{
Case ( Char ) 13 :
SB. append ( "   " );
Break ;
Case ( Char ) 10 :
SB. append ( "   " );
Break ;
Default :
SB. append (input1 [I]);
Break ;
}
}
Input1 = SB. tostring ();
}
Sw1.stop ();


WL (sw1.elapsedmilliseconds +   " Millisecond " );


RL ();
}

Code 2:

Public   Static   Void Main ()
{

String Input1 =   "" ;


Stringbuilder SB1 =   New Stringbuilder ();
For ( Int I = 0 ; I <   50000 ; I ++ )
{
Sb1.append ( " Wo \ r " );
}

Input1 = Sb1.tostring ();

WL ( " Replace test date is complate .. " );

Stopwatch SW1 =   New Stopwatch ();


// Char
Sw1.start ();
Input1 = Input1.replace ( " \ R " , "   " );
Input1 = Input1.replace ( " \ N " , "   " );
Sw1.stop ();


WL (sw1.elapsedmilliseconds +   " Millisecond " );


RL ();
}

Figure1: 6 s
Figure2: 3 S

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.