测试使用的环境:vs2019+.net core3.1
原文地址:https://www.cnblogs.com/hhhnicvscs/p/14296715.html
反驳第一条:如何检查代码中的空字符串?
调整顺序后输出,如下图:
上图调整顺序后,发现耗时排序发生了逆转。为什么?自行研究。
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(); } } }