C#編程實踐--字串反轉

來源:互聯網
上載者:User

標籤:os   ar   使用   for   sp   on   art   問題   代碼   

樸素反轉

樸素解法,倒序遍曆,字串拼接,字串效能低下,在長度已知的前提可以使用char數組代替

public static string NaiveReverse(string text){    string reverse = string.Empty;    for (int i = text.Length - 1; i >= 0; i--)    {        reverse += text[i];    }    return reverse;}
StringBuilder拼接

進一步改進,使用StringBuilder進行拼接字串

public static string SBReverse(string text){    StringBuilder builder = new StringBuilder(text.Length);    for (int i = text.Length - 1; i >= 0; i--)    {        builder.Append(text[i]);    }    return builder.ToString();}
二分反轉

遍曆次數降低到一半,效果如何?

public static string BinaryReverse(string text){    char[] charArray = text.ToCharArray();    int len = text.Length - 1;    for (int i = 0; i < len; i++, len--)    {        char tmp = charArray[i];        charArray[i] = charArray[len];        charArray[len] = tmp;    }    return new string(charArray);}
指標操作

咦?字串居然可變?

public static unsafe string UnsafeReverse(string text){    fixed (char* pText = text)    {        char* pStart = pText;        char* pEnd = pText + text.Length - 1;        for (int i = text.Length / 2; i >= 0; i--)        {            char temp = *pStart;            *pStart++ = *pEnd;            *pEnd-- = temp;        }        return text;    }}
數組反轉

最容易理解的方式,往往是最高效的,為啥這麼高效?

public static string ArrayReverse(string text){    char[] charArray = text.ToCharArray();    Array.Reverse(charArray);    return new string(charArray);}
XOR操作

是不是很有逼格?其實對於理解位操作還是有點協助,至於效能嘛。。。

public static string XorReverse(string text){    char[] charArray = text.ToCharArray();    int len = text.Length - 1;    for (int i = 0; i < len; i++, len--)    {        charArray[i] ^= charArray[len];        charArray[len] ^= charArray[i];        charArray[i] ^= charArray[len];    }    return new string(charArray);}
FCL實現

升級到.NET3.5了嗎?OK,最少的代碼實現,可是效能嘛,額

public static string EnumReverse(string text){    char[] reverse = text.Reverse().ToArray();    return new string(reverse);}

 

測試
Stopwatch watcher = new Stopwatch();// 字串規模int[] sizes = new[] { 10, 100, 1000, 10000 };// 反轉方法列表var ReverseMethods = new Func<string, string>[]{    NaiveReverse,    SBReverse,    BinaryReverse,    UnsafeReverse,    ArrayReverse,    XorReverse,    EnumReverse};for (int i = 0; i < sizes.Length; i++){    string text = new string(‘X‘, sizes[i]);    Console.WriteLine("For Size: {0}", sizes[i]);    for (int j = 0; j < ReverseMethods.Length; j++)    {        var invoker = ReverseMethods[j];        watcher.Restart();        invoker(text);        watcher.Stop();        Console.WriteLine("{0} Ticks: {1}", invoker.Method.Name, watcher.ElapsedTicks);    }    Console.WriteLine();}Console.ReadLine();

 

結語

寫這些代碼到底有什麼意義?效能到底如何?好了,那麼問題來了

C#編程實踐--字串反轉

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.