1
Vote

Speed of OrderedSet<T> and Microsoft's

description

Let us look at the following code
int n = 1000000;
            var x = new int[n];
            Random r = new Random();
            for (int i = 0; i < n; ++i)
                x[i] = r.Next(100000);

            Stopwatch s = new Stopwatch();

            // let us test Microsoft's sorted set
            s.Start();
            SortedSet<int> set1 = new SortedSet<int>();
            for (int i = 0; i < n; ++i)
                set1.Add(x[i]);
            s.Stop();
            Console.WriteLine(s.Elapsed);
            Console.WriteLine(set1.Count);

            //  and not lwt us test Wintellect's OrderedSet (the same red black tree)
            s.Start();
            OrderedSet<int> set2 = new OrderedSet<int>();
            for (int i = 0; i < n; ++i)
                set2.Add(x[i]);
            s.Stop();
            Console.WriteLine(s.Elapsed);
            Console.WriteLine(set2.Count);
The result is as following:

00:00:00.8194165
99992
00:00:03.3726086
99992

So, Microsoft't OrderedSet is 4.1 times faster than Wintellect's.

Can anyone explain this? Maybe I did not take into account some conditions?

comments