在编码时尽量使用ToUpper比较,避免使用ToLower,因为微软对ToUpper进行了优化,以下为测试结果:
public void TestToLower() { Stopwatch watch = new Stopwatch(); Int32 count = 1000 * 1000; // ToUpper测试开始 String lowerStr = "abcdefghijklmnopqrstuvwxyz"; watch = Stopwatch.StartNew(); for (int i = 0; i < count; i++) { lowerStr.ToUpper(); } Console.WriteLine("ToUpper:{0}", watch.Elapsed.ToString()); //ToLower测试开始 String upperStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; watch = Stopwatch.StartNew(); for (int i = 0; i < count; i++) { upperStr.ToLower(); } Console.WriteLine("ToLower:{0}", watch.Elapsed.ToString()); }
结果为:
ToUpper:00:00:00.2598023 ToLower:00:00:00.2697799