Loading

C# Hashtable VS. Dictionary 性能对比

Hashtable VS Dictionary

  • 因为Hashtable的Key和Value都是object类型,所以在使用值类型的时候,必然会出现装箱和拆箱的操作,因此性能肯定是不如Dictionary的,在此就不做过多比较了。

在此仅比较<string,string>的情况

class Program
    {
        static void Main(string[] args)
        {

            int nTimes = 10000;



            //排除定时器启动误差
            Stopwatch sw_D = new Stopwatch();

            sw_D.Restart();

            for (int i = 0; i < 10; i++)
            {
                Thread.Sleep(100);
            }

            sw_D.Stop();



            //Dictionary部分
            Dictionary<string, string> dict = new Dictionary<string, string>();

            sw_D.Restart();

            for (int i = 0; i < nTimes; i++)
            {
                string str = i.ToString();

                dict.Add(str, str);
            }

            sw_D.Stop();

            decimal decTime_D = sw_D.ElapsedTicks / (decimal)Stopwatch.Frequency;

            Console.WriteLine(decTime_D);




            sw_D.Restart();

            for (int i = 0; i < nTimes; i++)
            {
                string str = dict[i.ToString()];
            }

            sw_D.Stop();

            decTime_D = sw_D.ElapsedTicks / (decimal)Stopwatch.Frequency;

            Console.WriteLine(decTime_D);




            //Hashtable部分
            Hashtable hashtable = new Hashtable();

            Stopwatch sw_H = new Stopwatch();


            sw_H.Restart();

            for (int i = 0; i < nTimes; i++)
            {
                string str = i.ToString();

                hashtable.Add(str, str);
            }

            sw_H.Stop();


            decimal decTime_H = sw_H.ElapsedTicks / (decimal)Stopwatch.Frequency;

            Console.WriteLine(decTime_H);

            sw_H.Restart();

            for (int i = 0; i < nTimes; i++)
            {
                object obj = hashtable[i.ToString()];
            }

            sw_H.Stop();

            decTime_H = sw_H.ElapsedTicks / (decimal)Stopwatch.Frequency;

            Console.WriteLine(decTime_H);

            Console.ReadKey();


        }
    }
  • 在10000的数量级
    第一次计算Dictionary的误差比较大,相差有1/2之多。
    总体来看,Hashtable在查询上比Dictionary要强
0.0016746
0.0021346
0.0015785
0.0011693
  • 在100000的数量级
    这一次,不管怎么样,Dictionary都要强于Hashtable,这就很奇怪了
0.0155579
0.0150943
0.0196156
0.0189904

而且很明显的,Dictionary的时间要小于之前的上一个数量级中的10倍,也就是在数据量较大的时候对性能做了优化?
相反,Hashtable的时间显然是要大于之前的10倍的,也就是占用内存变大了很多之后,hashtable的性能降低了很多。

为了继续验证是不是在数据量较小的时候,是不是Hashtable性能更优,再测试一下100的数量级

  • 在100的数量级
    很明显,Hashtable要远强于Dictionary。
0.0001577
0.0000612
0.0000435
0.0000344

总结

在都是引用类型的情况下,数量级较小,可以将Dictionary改成Hashtable使用。数量级较大,建议选择Dictionary。

至于为什么在大数量级别下会出现“反转”,这个还有待后面考量。

不过可以预见的是,在WPF中依赖属性都存在于一张Hashtable,在数量较小的时候,是没啥问题的。

posted @ 2020-11-20 10:22  ligiggy  阅读(1775)  评论(1编辑  收藏  举报