Comparison of. net String Array search efficiency,. net String Array search
The following code is used:
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 (Co MpareStringInArrar (inputStr, arr) {Console. writeLine ("true2");} // Console. writeLine ("10 million string array element searches:"); System. threading. thread. sleep (3000); long ticks = DateTime. now. ticks; for (int I = 0; I <10000000; I ++) {CompareStringInArrar (inputStr, arr);} Console. writeLine ("Custom array query (Ticks): {0}", DateTime. now. ticks-ticks ); //////////////////////////////////////// system. threa Ding. thread. sleep (3000); ticks = DateTime. now. ticks; for (int I = 0; I <10000000; I ++) {FindStringInArrar0 (ref findStr, arr);} Console. writeLine ("the direct object address is equal for Array search (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. writeLin E ("directly traverse the array to find (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 ("Mixed Array query (Ticks): {0}", DateTime. now. ticks-ticks); Console. read (); // DBMethod ();} private static bool FindStringInArrar0 (ref string inputStr, str Ing [] 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 = Indium UtStr. 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> /// hybrid search /// </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 ;}
However, the case-sensitivity comparison is usually ignored, so you cannot simply use this method for comparison. Use the following test code:
Class Program {static void Main (string [] args) {string A = "124Abc"; string B = "2345b"; string C = "124 abce"; 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 (); // push for (int I = 0; I <1000000; I ++) {bool b1 = string. compare (A, B, StringComparison. ordinalIgnoreCase) = 0;} // start 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 =. 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 ();}}
The final conclusion is that the last method is the fastest, because the length is determined first. If the length is not the same, the comparison is skipped. The following is the test data:
input string(123Abc):123Abc87806,15255,58227,7569,1939