using System; using System.Linq; using System.Text; using System.Collections.Generic; public class Test { public static void Main() { Random rnd = new Random(123); StringBuilder sb = new StringBuilder(); string text = sb.ToString(); for (int i = 0; i < 1000000; i++) sb.Append((char)(rnd.Next(26) + 63)); text = sb.ToString(); double start, linqtime, dictionarytime; start = System.DateTime.Now.Ticks; var linq = text.GroupBy(c => c); foreach (var a in linq) { Console.WriteLine("{0} = {1}", a.Key, a.Count()); } linqtime = System.DateTime.Now.Ticks - start; start = System.DateTime.Now.Ticks; Dictionary counts = new Dictionary(); for (int i = 0; i < text.Length; i++) if (counts.ContainsKey(text[i])) counts[text[i]]++; else counts.Add(text[i], 1); foreach (var count in counts) Console.WriteLine("{0} = {1}", count.Key, count.Value); dictionarytime = System.DateTime.Now.Ticks - start; Console.WriteLine("LINQ time : {1}\nDictionary time : {2}\nLINQ is {0}% quicker" , dictionarytime/linqtime * 100 - 100, linqtime, dictionarytime); } }