原文地址:https://www.cnblogs.com/hhhnicvscs/p/14296715.htmlhtml
調整順序後輸出,以下圖:測試
上圖調整順序後,發現耗時排序發生了逆轉。爲何?自行研究。ui
using System; using System.Diagnostics; namespace ConsoleApp1 { public class Compare { public static void First(string Value) { if (Value.Length != 0) { } } public static void Second(string Value) { if (Value != "") { } } public static void Third(string Value) { if (Value != string.Empty) { } } } class Program { static void Main(string[] args) { Stopwatch sw = new Stopwatch(); sw.Start(); Compare.Third("weuroovnuifbbno"); sw.Stop(); Console.WriteLine("Using Empty: " + sw.ElapsedTicks); sw.Restart(); Compare.Second("weuroovnuifbbno"); sw.Stop(); Console.WriteLine("Using != " + sw.ElapsedTicks); sw.Restart(); Compare.First("weuroovnuifbbno"); sw.Stop(); Console.WriteLine("Using Length: " + sw.ElapsedTicks); Console.WriteLine("---------------"); sw.Restart(); Compare.First("joigjoerjgiogro"); sw.Stop(); Console.WriteLine("Using Length: " + sw.ElapsedTicks); sw.Restart(); Compare.Second("joigjoerjgiogro"); sw.Stop(); Console.WriteLine("Using != " + sw.ElapsedTicks); sw.Restart(); Compare.Third("joigjoerjgiogro"); sw.Stop(); Console.WriteLine("Using Empty: " + sw.ElapsedTicks); Console.ReadLine(); } } }
一樣的對順序進行調整,以下圖:spa
結果如圖,我也不說什麼了。.net
最後,網上的文章,不要盡信之,最好是本身可以實踐一下,這樣才能去其糟泊取其精華,別讓本身誤入歧途。pwa