.net字符串数组查找方式效率比较
下面是代码:
static void Main(string[] args) { string[] arr = new string[] { "AAA", "BBBB", "CCCC", "DDDD", "EEEEEE", "ffffff", "ggggggg", "hhhhhh", "iii", "", "jjj", "kkk" }; string findStr = "kkk"; for (int i = 0; i < arr.Length; i++) { if (object.ReferenceEquals(findStr, arr[i])) Console.Write(" true1 "); } Console.WriteLine(); Console.Write("input string kkk :"); string inputStr = Console.ReadLine(); if (CompareStringInArrar( inputStr, arr)) { Console.WriteLine(" true2 "); } /////////////////////// Console.WriteLine("一千万次字符串数组元素查找:"); System.Threading.Thread.Sleep(3000); long ticks = DateTime.Now.Ticks; for (int i = 0; i < 10000000; i++) { CompareStringInArrar(inputStr, arr); } Console.WriteLine("自定义数组查找(Ticks): {0}", DateTime.Now.Ticks - ticks); //////////////////////////////////////// System.Threading.Thread.Sleep(3000); ticks = DateTime.Now.Ticks; for (int i = 0; i < 10000000; i++) { FindStringInArrar0(ref findStr, arr); } Console.WriteLine("直接对象地址相等进行数组查找(Ticks):{0}", DateTime.Now.Ticks - ticks); /////////////////////////////////// System.Threading.Thread.Sleep(3000); ticks = DateTime.Now.Ticks; for (int i = 0; i < 10000000; i++) { FindStringInArrar(ref inputStr, arr); } Console.WriteLine("直接遍历进行数组查找(Ticks): {0}", DateTime.Now.Ticks - ticks); /////////////////////////////////// System.Threading.Thread.Sleep(3000); ticks = DateTime.Now.Ticks; for (int i = 0; i < 10000000; i++) { CompareStringInArrar2( inputStr, arr); } Console.WriteLine("混合数组查找(Ticks): {0}", DateTime.Now.Ticks - ticks); Console.Read(); //DBMethod(); } private static bool FindStringInArrar0(ref string inputStr, string[] arr) { for (int j = 0; j < arr.Length; j++) { if (object.ReferenceEquals(inputStr, arr[j])) return true; } return false; } private static bool FindStringInArrar(ref string inputStr, string[] arr) { for (int j = 0; j < arr.Length; j++) { if (inputStr == arr[j]) return true; } return false; } private static bool CompareStringInArrar( string inputStr, string[] arr) { //char[] inputCharArr = inputStr.ToCharArray(); int length = inputStr.Length; bool flag = true; //string strTemp = null; for (int i = 0; i < arr.Length; i++) { //strTemp = arr[i]; if (length == arr[i].Length) { flag = true; for (int j = 0; j < length; j++) { if (inputStr[j] != arr[i][j]) { flag = false; break; } } if (flag) return true; } } return false; } /// <summary> /// 混合查找 /// </summary> /// <param name="inputStr"></param> /// <param name="arr"></param> /// <returns></returns> private static bool CompareStringInArrar2(string inputStr, string[] arr) { //char[] inputCharArr = inputStr.ToCharArray(); int length = inputStr.Length; bool flag = true; //string strTemp = null; for (int i = 0; i < arr.Length; i++) { if (object.ReferenceEquals(inputStr, arr[i])) return true; //strTemp = arr[i]; if (length == arr[i].Length) { flag = true; for (int j = 0; j < length; j++) { if (inputStr[j] != arr[i][j]) { flag = false; break; } } if (flag) return true; } } return false; }
但是通常比较都需要忽略大小写比较,所以不能单纯使用这种方式比较,用下面的测试代码:
class Program { static void Main(string[] args) { string A = "124Abc"; string B = "2345b"; string C = "124abce"; Console.WriteLine("input string(123Abc):"); string D = Console.ReadLine(); string E = "124Abc"; long ticks = 0; long ticks2 = 0; long ticks3 = 0; long ticks4 = 0; long ticks5 = 0; Stopwatch sw = Stopwatch.StartNew(); //预热 for (int i = 0; i < 1000000; i++) { bool b1 = string.Compare(A, B, StringComparison.OrdinalIgnoreCase) == 0; } //开始 sw.Reset(); sw.Start(); for (int i = 0; i < 1000000; i++) { bool b1 = string.Compare(A, D, StringComparison.OrdinalIgnoreCase) == 0; } ticks = sw.ElapsedTicks; sw.Reset(); sw.Start(); for (int i = 0; i < 1000000; i++) { bool b2 = string.Equals(A, C, StringComparison.OrdinalIgnoreCase); } ticks2 = sw.ElapsedTicks; sw.Reset(); sw.Start(); for (int i = 0; i < 1000000; i++) { bool b2 = string.Equals(A, D, StringComparison.OrdinalIgnoreCase); } ticks3 = sw.ElapsedTicks; sw.Reset(); sw.Start(); for (int i = 0; i < 1000000; i++) { bool b2 = string.Equals(A, E, StringComparison.OrdinalIgnoreCase); } ticks4 = sw.ElapsedTicks; sw.Reset(); sw.Start(); for (int i = 0; i < 1000000; i++) { bool b2 = A.Length== C.Length && string.Equals(A, C, StringComparison.OrdinalIgnoreCase); } ticks5 = sw.ElapsedTicks; Console.WriteLine("{0},\r\n{1},\r\n{2},\r\n{3},\r\n{4}\r\n", ticks, ticks2, ticks3, ticks4, ticks5); Console.Read(); } }
最后得出结论,最后一种方式最快,原因在于首先判断了长度,如果不是一样的长度直接跳过比较,下面是测试数据:
input string(123Abc): 123Abc 87806, 15255, 58227, 7569, 1939