Fyi ..
Using:
private static HashSet<string> strHashSet = new HashSet<string>() { "0string", "1string", "2string", "3string", "4string", "5string", "6string", "7string", "8string", "9string", "Astring", "Bstring" }; private static List<string> strList = strHashSet.ToList(); private static string[] strArray = strList.ToArray(); private static Dictionary<int, string> strHashDict = strHashSet.ToDictionary(h => h.GetHashCode()); private static Dictionary<string, string> strDict = strHashSet.ToDictionary(h => h);
Checking the first and last lines in a list, and then checking a line that is not on the list: "xstring" Performs 500,000 iterations, all in milliseconds.
1.A Test: result = (str == "0string" || str == "1string" ... [storage var] [first]:[ last ]:[ none ]:[average] strArray 3.78 : 45.90 : 57.77 : 35.82 2.A Test: ExistsInList(string); [storage var] [first]:[ last ]:[ none ]:[average] none 36.14 : 28.97 : 24.02 : 29.71 3.A Test: .ContainsKey(string.GetHashCode()); [storage var] [first]:[ last ]:[ none ]:[average] strHashDict 34.86 : 28.41 : 21.46 : 28.24 4.A Test: .ContainsKey(string); [storage var] [first]:[ last ]:[ none ]:[average] strDict 38.99 : 32.34 : 22.75 : 31.36 5.A Test: .Contains(string); [storage var] [first]:[ last ]:[ none ]:[average] strHashSet 39.54 : 34.78 : 24.17 : 32.83 strList 23.36 : 122.07 : 127.38 : 90.94 strArray 350.34 : 426.29 : 426.05 : 400.90 6.A Test: .Any(p => p == string); [storage var] [first]:[ last ]:[ none ]:[average] strHashSet 75.70 : 331.38 : 339.40 : 248.82 strList 72.51 : 305.00 : 319.29 : 232.26 strArray 38.49 : 213.63 : 227.13 : 159.75
Interesting (if not unexpected) results when changing rows in a list:
private static HashSet<string> strHashSet = new HashSet<string>() { "string00", "string01", "string02", "string03", "string04", "string05", "string06", "string07", "string08", "string09", "string10", "string11" };
With "string99" as a check, no.
1.B Test: result = (str == "string00" || str == "string01" ... [storage var] [first]:[ last ]:[ none ]:[average] strArray 85.45 : 87.06 : 91.82 : 88.11 2.B Test: ExistsInList(string); [storage var] [first]:[ last ]:[ none ]:[average] none 30.12 : 27.97 : 21.36 : 26.48 3.B Test: .ContainsKey(string.GetHashCode()); [storage var] [first]:[ last ]:[ none ]:[average] strHashDict 32.51 : 28.00 : 20.83 : 27.11 4.B Test: .ContainsKey(string); [storage var] [first]:[ last ]:[ none ]:[average] strDict 36.45 : 32.13 : 22.39 : 30.32 5.B Test: .Contains(string); [storage var] [first]:[ last ]:[ none ]:[average] strHashSet 37.29 : 34.33 : 23.56 : 31.73 strList 23.34 : 147.75 : 153.04 : 108.04 strArray 349.62 : 460.19 : 459.99 : 423.26 6.B Test: .Any(p => p == string); [storage var] [first]:[ last ]:[ none ]:[average] strHashSet 76.26 : 355.09 : 361.31 : 264.22 strList 70.20 : 332.33 : 341.79 : 248.11 strArray 37.23 : 234.70 : 251.81 : 174.58
For cases where A and B look like tests 2 and 3 take precedence.
However, HashSet.Contains (string) is very efficient, not executed by the contents of the list, and has clear syntax ... might be a better choice.
Yes, it's true, I have no life.