当前位置: 移动技术网 > IT编程>开发语言>c# > C# Hashtable/Dictionary写入和读取对比详解

C# Hashtable/Dictionary写入和读取对比详解

2019年07月18日  | 移动技术网IT编程  | 我要评论
一:hashtable1.hashtable是一种散列表,他内部维护很多对key-value键值对,其还有一个类似索引的值叫做散列值(hashcode),它是根据getha

一:hashtable
1.hashtable是一种散列表,他内部维护很多对key-value键值对,其还有一个类似索引的值叫做散列值(hashcode),它是根据gethashcode方法对key通过一定算法获取得到的,所有的查找操作定位操作都是基于散列值来实现找到对应的key和value值的。
2.我们需要使用一个算法让散列值对应hashtable的空间地址尽量不重复,这就是散列函数(gethashcode)需要做的事。
3.当一个hashtable被占用一大半的时候我们通过计算散列值取得的地址值可能会重复指向同一地址,这就是哈希冲突。
在.net中键值对在hashtable中的位置position= (hashcode& 0x7fffffff) % hashtable.length,.net中是通过探测法解决哈希冲突的,当通过散列值取得的位置postion以及被占用的时候,就会增加一个位移x值判断下一个位置postion+x是否被占用,如果仍然被占用就继续往下位移x判断position+2*x位置是否被占用,如果没有被占用则将值放入其中。当hashtable中的可用空间越来越小时,则获取得到可用空间的难度越来越大,消耗的时间就越多。
4.当前hashtable中的被占用空间达到一个百分比的时候就将该空间自动扩容,在.net中这个百分比是72%,也叫.net中hashtable的填充因子为0.72。例如有一个hashtable的空间大小是100,当它需要添加第73个值的时候将会扩容此hashtable.
5.这个自动扩容的大小是多少呢?答案是当前空间大小的两倍最接近的素数,例如当前hashtable所占空间为素数71,如果扩容,则扩容大小为素数131.

二:dictionary

1.dictionary是一种变种的hashtable,它采用一种分离链接散列表的数据结构来解决哈希冲突的问题。
2.分离链接散列表是当散列到同一个地址的值存为一个链表中。
3.这个变种hashtable的填充因子是1

三:本文将以代码的形式探索hashtable和dictionary的插入和三种读取方式的效率(for/foreach/getenumerator)

复制代码 代码如下:

public class hashtabletest
    {
        static hashtable _hashtable;
        static dictionary<string, object> _dictionary;
        static void main()
        {
            compare(10);
            compare(10000);
            compare(5000000);
            console.readline();
        }
        public static void compare(int datacount)
        {
            console.writeline("-------------------------------------------------\n");
            _hashtable = new hashtable();
            _dictionary = new dictionary<string, object>();
            stopwatch stopwatch = new stopwatch();
            //hashtable插入datacount条数据需要时间
            stopwatch.start();
            for (int i = 0; i < datacount; i++)
            {
                _hashtable.add("str" + i.tostring(), "value");
            }
            stopwatch.stop();
            console.writeline(" hashtable插入" + datacount + "条数据需要时间:" + stopwatch.elapsed);

            //dictionary插入datacount条数据需要时间
            stopwatch.reset();
            stopwatch.start();
            for (int i = 0; i < datacount; i++)
            {
                _dictionary.add("str" + i.tostring(), "value");
            }
            stopwatch.stop();
            console.writeline(" dictionary插入" + datacount + "条数据需要时间:" + stopwatch.elapsed);

            //dictionary插入datacount条数据需要时间
            stopwatch.reset();
            int si = 0;
            stopwatch.start();
            for(int i=0;i<_hashtable.count;i++)
            {
                si++;
            }
            stopwatch.stop();
            console.writeline(" hashtable遍历时间:" + stopwatch.elapsed + " ,遍历采用for方式");

            //dictionary插入datacount条数据需要时间
            stopwatch.reset();
            si = 0;
            stopwatch.start();
            foreach (var s in _hashtable)
            {
                si++;
            }
            stopwatch.stop();
            console.writeline(" hashtable遍历时间:" + stopwatch.elapsed + " ,遍历采用foreach方式");

            //dictionary插入datacount条数据需要时间
            stopwatch.reset();
            si = 0;
            stopwatch.start();
            idictionaryenumerator _hashenum = _hashtable.getenumerator();
            while (_hashenum.movenext())
            {
                si++;
            }
            stopwatch.stop();
            console.writeline(" hashtable遍历时间:" + stopwatch.elapsed + " ,遍历采用hashtable.getenumerator()方式");

            //dictionary插入datacount条数据需要时间
            stopwatch.reset();
            si = 0;
            stopwatch.start();
            for(int i=0;i<_dictionary.count;i++)
            {
                si++;
            }
            stopwatch.stop();
            console.writeline(" dictionary遍历时间:" + stopwatch.elapsed + " ,遍历采用for方式");

            //dictionary插入datacount条数据需要时间
            stopwatch.reset();
            si = 0;
            stopwatch.start();
            foreach (var s in _dictionary)
            {
                si++;
            }
            stopwatch.stop();
            console.writeline(" dictionary遍历时间:" + stopwatch.elapsed + " ,遍历采用foreach方式");

            //dictionary插入datacount条数据需要时间
            stopwatch.reset();
            si = 0;
            stopwatch.start();
            _hashenum = _dictionary.getenumerator();
            while (_hashenum.movenext())
            {
                si++;
            }
            stopwatch.stop();
            console.writeline(" dictionary遍历时间:" + stopwatch.elapsed + " ,遍历采用dictionary.getenumerator()方式");


            console.writeline("\n-------------------------------------------------");
        }
    }




四:从上面的结果可以看出

1.hashtable大数据量插入数据时需要花费比dictionary大的多的时间。
2.for方式遍历hashtable和dictionary速度最快。
3.在foreach方式遍历时dictionary遍历速度更快。
五:在单线程的时候使用dictionary更好一些,多线程的时候使用hashtable更好。
因为hashtable可以通过hashtable tab = hashtable.synchronized(new hashtable());获得线程安全的对象。
当然因为各自电脑的情况不一样,可能会有部分误差。如有问题,敬请斧正。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网