About the-C # string reversal methods and comparison of performance C # string reversal methods and Performance Comparison

Source: Internet
Author: User
Original article: C # string reversal methods and Performance Comparison
 
I think it is strange when I see it. In many cases, the class library method must have better performance than the method I wrote.
 
The string input by the original author. If it is an array of input characters, the test result is much more unexpected.
ModifyCodeAs follows:
Class stringreverse {// <summary> // string reversal-stringbuilder implementation /// </Summary> /// <Param name = "str"> </param>/ // <returns> </returns> Public static string reverseusingstringbuilder (char [] Str) {int length = Str. length; stringbuilder sb = new stringbuilder (length); For (INT I = length-1; I> = 0; I --) {sb. append (STR [I]);} return sb. tostring ();} /// <summary> // string reversal-chararray implementation /// </Summary> /// <Param name = "str"> </param> // <returns> </returns> Public static string reverseusingchararray (char [] Str) {char [] arr = STR; array. reverse (ARR); return new string (ARR); // return string. join (string. empty, str. reverse ());} /// <summary> /// reverse string-XOR or implementation /// </Summary> /// <Param name = "str"> </param> /// <returns> </returns> Public static string reverseusingxor (char [] Str) {char [] arr = STR; int L = arr. length-1; // exchange value for (INT I = 0; I <L; I ++, l --) {STR [I] ^ = STR [l]; STR [l] ^ = STR [I]; STR [I] ^ = STR [l];} return new string (STR );} public Delegate string funcdelegate (char [] S ); /// <summary> /// test method /// </Summary> /// <Param name = "Description"> method description </param> /// <Param name = "func"> test method </param> /// <Param name = "times"> Number of executions </param> /// <Param name = "str"> test string </param> Public static void benchmark (string description, funcdelegate func, int times, char [] Str) {stopwatch Sw = new stopwatch (); Sw. start (); For (Int J = 0; j <times; j ++) {func (STR);} SW. stop (); console. writeline ("method {0}: Call {1} Times, in use {2 }. ", description, times, SW. elapsedticks );} /// <summary> /// generate a random string of the specified length /// </Summary> /// <Param name = "length"> length of the string </param>/ // <returns> </returns> Public static char [] randomstring (INT length) {random = new random (); // stringbuilder sb = new stringbuilder (); char [] sb = new char [length]; for (INT I = 0; I <length; I ++) {Sb [I] = convert. tochar (convert. toint32 (math. floor (26 * random. nextdouble () + 65);} return sb;} static void main (string [] ARGs) {int [] lengths = new int [] {1, 10, 15, 25, 50, 75,100,100 0, 100000}; // int [] lengths = new int [] {1, 10, 15, 25, 50, 75,100 }; foreach (INT Len in lengths) {// each method is executed 10000 times, and INT iterations = 10000 is precise; // generate a random test string char [] teststring = stringreverse. randomstring (LEN); // print the test information stringreverse. benchmark (string. format ("string Builder (test string length: {0})", Len), stringreverse. reverseusingstringbuilder, iterations, teststring); stringreverse. benchmark (string. format ("array. reverse (test string length: {0}) ", Len), stringreverse. reverseusingchararray, iterations, teststring); stringreverse. benchmark (string. format ("XOR (test string length: {0})", Len), stringreverse. reverseusingxor, iterations, teststring); console. writeline ();} console. read ();}}

Test results:

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.