Efficiency Comparison of several string inversion methods

Source: Internet
Author: User
Static   String Reverse1 ( String Original)
... {< br> char [] arr = original. tochararray ();
array. reverse (ARR);
return New string (ARR );
}

Static   String Revease21 ( String Original)
... {
Int Length = Original. length;
Char [] Arr =   New   Char [Length];
For ( Int I =   0 ; I < (Length & ( ~ 3 ); I + =   4 )
... {
Arr [I] = Original [Length - I -   1 ];
Arr [I + 1 ] = Original [Length - I -   2 ];
Arr [I + 2 ] = Original [Length - I -   3 ];
Arr [I + 3 ] = Original [Length - I -   4 ];
}
For ( Int I = Length & ( ~ 3 ); I < Length; I ++ )
... {
Arr [I]=Original [Length-I- 1];
}
Return   New   String (ARR );
}

Static   String Revease22 ( String Original)
... {
Int Length = Original. length;
Char [] Arr =   New   Char [Length];
For ( Int I =   0 ; I < Length; I ++ )
... {
Arr [I]=Original [Length-I- 1];
}
Return   New   String (ARR );
}

Static   String Revease3 ( String Original)
... {
Int Length = Original. length;
Stringbuilder sb =   New Stringbuilder (length );
For ( Int I = Length - 1 ; I > =   0 ; I -- )
SB. append (original [I]);
Return SB. tostring ();
}

In revease1 (), char [] is assigned two values (tochararray () and array. revease), so I have come up with the revease2 and revease3 () methods. Below is a simple performance test for these four methods.Code:

Static   Void Main ( String [] ARGs)
... {
String Teststring =   " Test string inversion Test string reverse test string reverse test string reverse Test " ;
Datetime start = Datetime. now;
For ( Int I =   0 ; I <   3000000 ; I ++ )
... {
StringS=Reverse1 (teststring );
}
Datetime end = Datetime. now;
Console. writeline ( " 1: " + (End - Start ));

Start = Datetime. now;
For ( Int I =   0 ; I <   3000000 ; I ++ )
... {
StringS=Revease21 (teststring );
}
End = Datetime. now;
Console. writeline ( " 21: "   + (End - Start ));

Start = Datetime. now;
For ( Int I =   0 ; I <   3000000 ; I ++ )
... {
StringS=Revease22 (teststring );
}
End = Datetime. now;
Console. writeline ( " 22: "   + (End - Start ));

Start = Datetime. now;
For ( Int I =   0 ; I <   3000000 ; I ++ )
... {
StringS=Revease3 (teststring );
}
End = Datetime. now;
Console. writeline ( " 3: "   + (End - Start ));

Console. Readline ();
}

The test results show that the revease1 () code is the most concise and the fastest running speed, revease21 () and revease22 () followed by revease3. It can be seen that tochararray () and array. revease () implemented in. NET Framework are still very efficient.

Turn: http://blog.csdn.net/happyhippy/archive/2006/10/30/1357692.aspx

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.