188金宝搏三种字符串反转方法效能相比,字符串反转

方法二:.NET3.5以上

附2:StringBuilder.Append()方法的源码(由Reflector.exe反汇编得到):

 

 Revease1()中对char[]进行了两次赋值(ToCharArray()和Array.Revease),所以我有想到了Revease2和Revease3()两种方法,下面是对这四种方法进行简单性能测试的代码:

 

1 :  00:00:03.4375000
21:  00:00:06.1250000
22:  00:00:09.9687500
3 :  00:01:05.5468750

public static string Reverse(string name)
{
   if (String.IsNullOrEmpty(name))
   {
      throw new Exception("字符串不能为空!");
   }
  char[] nm = name.ToCharArray();
  for (int i = 0; i < (nm.Length-1 )/ 2; i++)
  {
     char q = nm[i];
     nm[i]= nm[nm.Length - 1 - i];
    nm[nm.Length - 1 - i] = q;
  }

Release:

public static string Reverse(string name)
{
     char[] reverse = name.Reverse().ToArray();

188金宝搏 1[ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
188金宝搏 2public static void Reverse(Array array)
188金宝搏 3188金宝搏 4...{
188金宝搏 5      if (array == null)
188金宝搏 6188金宝搏 7      ...{
188金宝搏 8            throw new ArgumentNullException("array");
188金宝搏 9      }
188金宝搏 10      Array.Reverse(array, array.GetLowerBound(0), array.Length);
188金宝搏 11}
188金宝搏 12
188金宝搏 13[ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
188金宝搏 14public static void Reverse(Array array, int index, int length)
188金宝搏 15188金宝搏 16...{
188金宝搏 17      int num1;
188金宝搏 18      int num2;
188金宝搏 19      if (array == null)
188金宝搏 20188金宝搏 21      ...{
188金宝搏 22            throw new ArgumentNullException("array");
188金宝搏 23      }
188金宝搏 24      if ((index < array.GetLowerBound(0)) || (length < 0))
188金宝搏 25188金宝搏 26      ...{
188金宝搏 27            throw new ArgumentOutOfRangeException((index < 0) ? "index" : "length", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
188金宝搏 28      }
188金宝搏 29      if ((array.Length - (index - array.GetLowerBound(0))) < length)
188金宝搏 30188金宝搏 31      ...{
188金宝搏 32            throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
188金宝搏 33      }
188金宝搏 34      if (array.Rank != 1)
188金宝搏 35188金宝搏 36      ...{
188金宝搏 37            throw new RankException(Environment.GetResourceString("Rank_MultiDimNotSupported"));
188金宝搏 38      }
188金宝搏 39      if (!Array.TrySZReverse(array, index, length))
188金宝搏 40188金宝搏 41      ...{
188金宝搏 42            num1 = index;
188金宝搏 43            num2 = (index + length) - 1;
188金宝搏 44            object[] objArray1 = array as object[];
188金宝搏 45            if (objArray1 == null)
188金宝搏 46188金宝搏 47            ...{
188金宝搏 48                  goto Label_00DE;
188金宝搏 49            }
188金宝搏 50            while (num1 < num2)
188金宝搏 51188金宝搏 52            ...{
188金宝搏 53                  object obj1 = objArray1[num1];
188金宝搏 54                  objArray1[num1] = objArray1[num2];
188金宝搏 55                  objArray1[num2] = obj1;
188金宝搏 56                  num1++;
188金宝搏 57                  num2--;
188金宝搏 58            }
188金宝搏 59      }
188金宝搏 60      return;
188金宝搏 61Label_00DE:
188金宝搏 62      if (num1 >= num2)
188金宝搏 63188金宝搏 64      ...{
188金宝搏 65            return;
188金宝搏 66      }
188金宝搏 67      object obj2 = array.GetValue(num1);
188金宝搏 68      array.SetValue(array.GetValue(num2), num1);
188金宝搏 69      array.SetValue(obj2, num2);
188金宝搏 70      num1++;
188金宝搏 71      num2--;
188金宝搏 72      goto Label_00DE;
188金宝搏 73}

     return new string(reverse);
}

188金宝搏 74public StringBuilder Append(string value)
188金宝搏 75188金宝搏 76...{
188金宝搏 77      if (value != null)
188金宝搏 78188金宝搏 79      ...{
188金宝搏 80            string text1 = this.m_StringValue;
188金宝搏 81            IntPtr ptr1 = Thread.InternalGetCurrentThread();
188金宝搏 82            if (this.m_currentThread != ptr1)
188金宝搏 83188金宝搏 84            ...{
188金宝搏 85                  text1 = string.GetStringForStringBuilder(text1, text1.Capacity);
188金宝搏 86            }
188金宝搏 87            int num1 = text1.Length;
188金宝搏 88            int num2 = num1 + value.Length;
188金宝搏 89            if (this.NeedsAllocation(text1, num2))
188金宝搏 90188金宝搏 91            ...{
188金宝搏 92                  string text2 = this.GetNewString(text1, num2);
188金宝搏 93                  text2.AppendInPlace(value, num1);
188金宝搏 94                  this.ReplaceString(ptr1, text2);
188金宝搏 95            }
188金宝搏 96            else
188金宝搏 97188金宝搏 98            ...{
188金宝搏 99                  text1.AppendInPlace(value, num1);
188金宝搏 100                  this.ReplaceString(ptr1, text1);
188金宝搏 101            }
188金宝搏 102      }
188金宝搏 103      return this;
188金宝搏 104}
188金宝搏 105
188金宝搏 106private bool NeedsAllocation(string currentString, int requiredLength)
188金宝搏 107188金宝搏 108...{
188金宝搏 109      return (currentString.ArrayLength <= requiredLength);
188金宝搏 110
188金宝搏 111
188金宝搏 112internal unsafe void AppendInPlace(string value, int currentLength)
188金宝搏 113188金宝搏 114...{
188金宝搏 115      int num1 = value.Length;
188金宝搏 116      int num2 = currentLength + num1;
188金宝搏 117      fixed (char* chRef1 = &this.m_firstChar)
188金宝搏 118188金宝搏 119      ...{
188金宝搏 120            fixed (char* chRef2 = &value.m_firstChar)
188金宝搏 121188金宝搏 122            ...{
188金宝搏 123                  string.wstrcpy(chRef1 + currentLength, chRef2, num1);
188金宝搏 124            }
188金宝搏 125            chRef1[num2] = '

方法一:

1 :  00:00:05.7812500
21:  00:00:07.4218750
22:  00:00:08.2500000
3 :  00:00:50.3593750

 

188金宝搏 126   static void Main(string[] args)
188金宝搏 127188金宝搏 128            ...{
188金宝搏 129                string testString = "测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转测试字符串反转";
188金宝搏 130                DateTime start = DateTime.Now;
188金宝搏 131                for (int i = 0; i < 3000000; i++)
188金宝搏 132188金宝搏 133                ...{
188金宝搏 134                    string s = Reverse1(testString);
188金宝搏 135                }
188金宝搏 136                DateTime end = DateTime.Now;
188金宝搏 137                Console.WriteLine("1 :  "+(end - start));
188金宝搏 138
188金宝搏 139                start = DateTime.Now;
188金宝搏 140                for (int i = 0; i < 3000000; i++)
188金宝搏 141188金宝搏 142                ...{
188金宝搏 143                    string s = Revease21(testString);
188金宝搏 144                }
188金宝搏 145                end = DateTime.Now;
188金宝搏 146                Console.WriteLine("21:  " + (end - start));
188金宝搏 147
188金宝搏 148                start = DateTime.Now;
188金宝搏 149                for (int i = 0; i < 3000000; i++)
188金宝搏 150188金宝搏 151                ...{
188金宝搏 152                    string s = Revease22(testString);
188金宝搏 153                }
188金宝搏 154                end = DateTime.Now;
188金宝搏 155                Console.WriteLine("22:  " + (end - start));
188金宝搏 156
188金宝搏 157                start = DateTime.Now;
188金宝搏 158                for (int i = 0; i < 3000000; i++)
188金宝搏 159188金宝搏 160                ...{
188金宝搏 161                    string s = Revease3(testString);
188金宝搏 162                }
188金宝搏 163                end = DateTime.Now;
188金宝搏 164                Console.WriteLine("3 :  " + (end - start));
188金宝搏 165
188金宝搏 166                Console.ReadLine();
188金宝搏 167            }

 

188金宝搏 168            static string Reverse1(string original)
188金宝搏 169188金宝搏 170            ...{
188金宝搏 171                char[] arr = original.ToCharArray();
188金宝搏 172                Array.Reverse(arr);
188金宝搏 173                return new string(arr);
188金宝搏 174            }
188金宝搏 175
188金宝搏 176            static string Revease21(string original)
188金宝搏 177188金宝搏 178            ...{
188金宝搏 179                int length = original.Length;
188金宝搏 180                char[] arr = new char[length];
188金宝搏 181                for (int i = 0; i < (length & (~3)); i += 4)
188金宝搏 182188金宝搏 183                ...{
188金宝搏 184                    arr[i] = original[length - i - 1];
188金宝搏 185                    arr[i+1] = original[length - i - 2];
188金宝搏 186                    arr[i+2] = original[length - i - 3];
188金宝搏 187                    arr[i+3] = original[length - i - 4];
188金宝搏 188                }
188金宝搏 189                for (int i = length & (~3); i < length; i++)
188金宝搏 190188金宝搏 191                ...{
188金宝搏 192                    arr[i] = original[length - i - 1];
188金宝搏 193                }
188金宝搏 194                return new string(arr);
188金宝搏 195            }
188金宝搏 196
188金宝搏 197            static string Revease22(string original)
188金宝搏 198188金宝搏 199            ...{
188金宝搏 200                int length = original.Length;
188金宝搏 201                char[] arr = new char[length];
188金宝搏 202                for (int i = 0; i < length; i++)
188金宝搏 203188金宝搏 204                ...{
188金宝搏 205                    arr[i] = original[length - i - 1];
188金宝搏 206                }
188金宝搏 207                return new string(arr);
188金宝搏 208            }
188金宝搏 209
188金宝搏 210            static string Revease3(string original)
188金宝搏 211188金宝搏 212            ...{
188金宝搏 213                int length = original.Length;
188金宝搏 214                StringBuilder sb = new StringBuilder(length);
188金宝搏 215                for (int i = length-1; i >= 0; i--)
188金宝搏 216                sb.Append(original[i]);
188金宝搏 217                return sb.ToString();
188金宝搏 218            }

方法三:二分法

Debug:

public static string Reverse(string name)
{
     if (String.IsNullOrEmpty(name))
       {
           throw new Exception("字符串不能为空!");
       }
    StringBuilder sb = new StringBuilder(name.Length);
    for (int i = name.Length-1; i >= 0; i--)
     {
        sb.Append(name[i]);
    }
        return sb.ToString();
}

面是实现字符串反转的四种方法:

  return new string(nm);

附1:Array.Revease()方法的源码(由Reflector.exe反汇编得到):

}

但还有个奇怪的问题,就是Debug版本中的Revease1()和Revease21()运行起来要比Release版本中的要快,而Revease22()和Revease3()就比较正常。按说Release时做了更多的优化工作,运行起来更快才对,迷惑ing...,下面是测试结果:

测试结果是Revease1()代码最简洁,运行速度也最快,Revease21()和Revease22()其次,Revease3()最慢。可见.net
framework中实现的ToCharArray()和Array.Revease()效率还是蛮高的^_^

 

相关文章

Comment ()
评论是一种美德,说点什么吧,否则我会恨你的。。。