using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; namespace dictionary_vs_switch { class Program { static void Main() { char[] fromArr = Enumerable.Range('a', 'z' - 'a' + 1).Select(i => (char)i).ToArray(); char[] iterArr = new char[10000]; Random r = new Random(); for (int i = 0; i < iterArr.Length; i++) { iterArr[i] = fromArr[r.Next(fromArr.Length)]; } var result1 = new StringBuilder(iterArr.Length); var result2 = new StringBuilder(iterArr.Length); Stopwatch s1 = new Stopwatch(); Stopwatch s2 = new Stopwatch(); s1.Start(); Dictionary d = new Dictionary { {'a', 'а'}, {'b', 'б'}, {'c', 'ц'}, {'d', 'д'}, {'e', 'е'}, {'f', 'ф'}, {'g', 'г'}, {'h', 'ш'}, {'i', 'и'}, {'j', 'й'}, {'k', 'к'}, {'l', 'л'}, {'m', 'м'}, {'n', 'н'}, {'o', 'о'}, {'p', 'п'}, {'q', 'ы'}, {'r', 'р'}, {'s', 'с'}, {'t', 'т'}, {'u', 'у'}, {'v', 'в'}, {'w', 'ю'}, {'x', 'ч'}, {'y', 'я'}, {'z', 'з'} }; foreach (char c in iterArr) { result1.Append(d[c]); } s1.Stop(); s2.Start(); foreach (char c in iterArr) { result2.Append(getChar(c)); } s2.Stop(); Console.WriteLine("dictionary: " + s1.ElapsedTicks); Console.WriteLine("switch: " + s2.ElapsedTicks); Console.ReadKey(); } private static char getChar(char c) { switch (c) { case 'a': return 'а'; case 'b': return 'б'; case 'c': return 'ц'; case 'd': return 'д'; case 'e': return 'е'; case 'f': return 'ф'; case 'g': return 'г'; case 'h': return 'ш'; case 'i': return 'и'; case 'j': return 'й'; case 'k': return 'к'; case 'l': return 'л'; case 'm': return 'м'; case 'n': return 'н'; case 'o': return 'о'; case 'p': return 'п'; case 'q': return 'ы'; case 'r': return 'р'; case 's': return 'с'; case 't': return 'т'; case 'u': return 'у'; case 'v': return 'в'; case 'w': return 'ю'; case 'x': return 'ч'; case 'y': return 'я'; case 'z': return 'з'; default: return ' '; } } } }