尽量使用ToUpper比较,避免使用ToLower

在编码时尽量使用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

posted @ 2013-09-04 17:52  小R  阅读(372)  评论(0编辑  收藏  举报