Ejemplo n.º 1
0
        public void TryGetValue()
        {
            string actual;
            var    dict = new Dictionary <int, string>();

            for (int i = 0; i < 10; i++)
            {
                dict[i] = i.ToString();
            }
            dict.TryGetValue(2, out actual);
            var sw = Stopwatch.StartNew();
            var n  = 1000000;

            for (int i = 0; i < n; i++)
            {
                dict.TryGetValue(i % 10, out actual);
            }

            sw.Stop();
            Console.WriteLine($"// {DateTime.Today.ToShortDateString()}| dict.TryGetValue(i % 10, out actual)       {n:N0} times {sw.ElapsedMilliseconds} ms");

            var cdict = new ConcurrentDictionary <int, string>();

            for (int i = 0; i < 10; i++)
            {
                cdict[i] = i.ToString();
            }

            cdict.TryGetValue(2, out actual);
            sw.Restart();
            for (int i = 0; i < n; i++)
            {
                cdict.TryGetValue(i % 10, out actual);
            }

            sw.Stop();
            Console.WriteLine($"// {DateTime.Today.ToShortDateString()}| cdict.TryGetValue(i % 10, out actual);     {n:N0} times {sw.ElapsedMilliseconds} ms");

            var slist = new SortedList <int, string>();

            for (int i = 0; i < 10; i++)
            {
                slist[i] = i.ToString();
            }

            slist.TryGetValue(2, out actual);
            sw.Restart();
            for (int i = 0; i < n; i++)
            {
                slist.TryGetValue(i % 10, out actual);
            }

            sw.Stop();
            Console.WriteLine($"// {DateTime.Today.ToShortDateString()}| slist.TryGetValue(i % 10, out actual);     {n:N0} times {sw.ElapsedMilliseconds} ms");

            var array = new KeyValuePair <int, string> [10];

            for (int i = 0; i < 10; i++)
            {
                array[i] = new KeyValuePair <int, string>(i, i.ToString());
            }

            var kvp = Array.Find(array, x => x.Key == 2);

            sw.Restart();
            for (int i = 0; i < n; i++)
            {
                kvp = Array.Find(array, x => x.Key == i % 10);
            }

            sw.Stop();
            Console.WriteLine($"// {DateTime.Today.ToShortDateString()}| Array.Find(array, x => x.Key == i % 10)    {n:N0} times {sw.ElapsedMilliseconds} ms");

            sw.Restart();
            var kvpComparer = new KvpComparer();

            for (int i = 0; i < n; i++)
            {
                var j = Array.BinarySearch(array, array[i % 10], kvpComparer);
            }

            sw.Stop();
            Console.WriteLine($"// {DateTime.Today.ToShortDateString()}| Array.BinarySearch(array, array[i % 10])   {n:N0} times {sw.ElapsedMilliseconds} ms");

            sw.Restart();

            for (int i = 0; i < n; i++)
            {
                var j = array.BinarySearchBy(x => x.Key, i % 10);
            }

            sw.Stop();
            Console.WriteLine($"// {DateTime.Today.ToShortDateString()}| array.BinarySearchBy(x => x.Key, i % 10    {n:N0} times {sw.ElapsedMilliseconds} ms");

            sw.Restart();

            for (int i = 0; i < n; i++)
            {
                kvp = array.First(x => x.Key == i % 10);
            }

            sw.Stop();
            Console.WriteLine($"// {DateTime.Today.ToShortDateString()}| array.First(x => x.Key == i % 10)          {n:N0} times {sw.ElapsedMilliseconds} ms");
        }