language: C# (mono-2.8)
date: 351 days 21 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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<char, int> counts = new Dictionary<char, int>();
                        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);
        }
}