关于-c#字符串三种逆转方法及性能比较的另外看法

原文:c#字符串三种逆转方法及性能比较

看到后觉得很奇怪,个人认为在很多情况下类库方法肯定要比自己写的方法性能好
原文作者传入的字符串,如果是传入字符数组的话测试结果则大大出乎作者的意外。
修改代码如下:
 class StringReverse
    {
        /// <summary>
        /// 字符串逆转-StringBuilder实现
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ReverseUsingStringBuilder(char[] str)
        {
            int length = str.Length;
            StringBuilder sb = new StringBuilder(length);

            for (int i = length - 1; i >= 0; i--)
            {
                sb.Append(str[i]);
            }
            return sb.ToString();
        }

        /// <summary>
        /// 字符串逆转-CharArray实现
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ReverseUsingCharArray(char[] str)
        {
            char[] arr = str;
            Array.Reverse(arr);
            return new string(arr);
           // return string.Join(string.Empty,str.Reverse());
          
        }
        /// <summary>
        /// 字符串逆转-异或实现
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ReverseUsingXor(char[] str)
        {
            char[] arr = str;
            int l = arr.Length-1;

            //交换值
            for (int i = 0; i < l; i++, l--)
            {
                str[i] ^= str[l];
                str[l] ^= str[i];
                str[i] ^= str[l];
            }
            return new string(str);
        }
        public delegate string funcDelegate(char[] s);

        /// <summary>
        /// 测试方法
        /// </summary>
        /// <param name="description">方法描述</param>
        /// <param name="func">测试方法</param>
        /// <param name="times">执行次数</param>
        /// <param name="str">测试字符串</param>
        public static void Benchmark(string description, funcDelegate func, int times, char[] str)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            for (int j = 0; j < times; j++)
            {
                func(str);
            }
            sw.Stop();
            Console.WriteLine("方法{0}:调用{1}次,用时{2}.", description, times, sw.ElapsedTicks);
        }
        /// <summary>
        /// 生成指定长度随机字符串
        /// </summary>
        /// <param name="length">字条串长度</param>
        /// <returns></returns>
        public static char[] RandomString(int length)
        {
            Random random = new Random();
           // StringBuilder sb = new StringBuilder();
            char[] sb=new char[length];
            for (int i = 0; i < length; i++)
            {
                sb[i]=Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            }
            return sb;
        }
        static void Main(string[] args)
        {
            int[] lengths = new int[] { 1, 10, 15, 25, 50, 75, 100, 1000, 100000 };
          //  int[] lengths = new int[] { 1, 10, 15, 25, 50, 75, 100};

            foreach (int len in lengths)
            {
                //每个方法都执行10000次,力求精确
                int iterations = 10000;

                //生成随机测试字符串
                char[] testString = StringReverse.RandomString(len);
          
                //打印测试信息
                StringReverse.Benchmark(String.Format("String Builder (测试字符串长度为{0})", len), StringReverse.ReverseUsingStringBuilder, iterations, testString);
                StringReverse.Benchmark(String.Format("Array.Reverse (测试字符串长度为{0})", len), StringReverse.ReverseUsingCharArray, iterations, testString);
                StringReverse.Benchmark(String.Format("Xor (测试字符串长度为{0})", len), StringReverse.ReverseUsingXor, iterations, testString);

                Console.WriteLine();
            }
            Console.Read();

        }
    }

  测试结果截图:

posted @ 2012-06-11 14:56  代码示例  阅读(851)  评论(3编辑  收藏  举报