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