C#:Dictionary<key,key>的Clear到底有多多块

有时我们希望复用一个Dictionary的时候,可以Clear()也可以,直接new一个新的对象,

两者到底是哪个划算呢?

我们看程序:

  private void Experiment()
        {

            System.Diagnostics.Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            //Dictionary<int, int> aa = new Dictionary<int, int>();

            for (var loop = 0; loop < 30000; loop++)
            {
                Dictionary<int, int> aa = new Dictionary<int, int>();
              
                
                for (var i = 0; i < 10000; i++)
                { aa.Add(i,i); }

                //aa.Clear();
                

            }

            stopwatch.Stop();

            Console.WriteLine("All timeL:"+stopwatch.Elapsed.TotalSeconds);
        }


数据结果如下:

使用Clear()时间为6秒。而使用new object的方法,时间为20秒,我觉得只所以这样

是因为NEW object的方案中,需要执行很多次Resize的操作。

这些东西看基类代码如下:

Add操作如下:

  public void Add(TKey key, TValue value)
        {
            this.Insert(key, value, true);
        }


其调用Insert操作,代码如下:

  private void Insert(TKey key, TValue value, bool add)
        {
            int freeList;
            if (key == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
            }
            if (this.buckets == null)
            {
                this.Initialize(0);
            }
            int num = this.comparer.GetHashCode(key) & 0x7fffffff;
            int index = num % this.buckets.Length;
            for (int i = this.buckets[index]; i >= 0; i = this.entries[i].next)
            {
                if ((this.entries[i].hashCode == num) && this.comparer.Equals(this.entries[i].key, key))
                {
                    if (add)
                    {
                        ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_AddingDuplicate);
                    }
                    this.entries[i].value = value;
                    this.version++;
                    return;
                }
            }
            if (this.freeCount > 0)
            {
                freeList = this.freeList;
                this.freeList = this.entries[freeList].next;
                this.freeCount--;
            }
            else//散列已满
            {
                if (this.count == this.entries.Length)
                {
                    this.Resize();//为散列重新开辟一块儿内存空间
                    index = num % this.buckets.Length;
                }
                freeList = this.count;
                this.count++;
            }
            this.entries[freeList].hashCode = num;
            this.entries[freeList].next = this.buckets[index];
            this.entries[freeList].key = key;
            this.entries[freeList].value = value;
            this.buckets[index] = freeList;
            this.version++;
        }


Resize()如下:

 private void Resize()
        {
            int prime = HashHelpers.GetPrime(this.count * 2);
            int[] numArray = new int[prime];//开辟新的数组
            for (int i = 0; i < numArray.Length; i++)
            {
                numArray[i] = -1;
            }
            Entry<TKey, TValue>[] destinationArray = new Entry<TKey, TValue>[prime];//开辟新的数组
            Array.Copy(this.entries, 0, destinationArray, 0, this.count);
            for (int j = 0; j < this.count; j++)
            {
                int index = destinationArray[j].hashCode % prime;
                destinationArray[j].next = numArray[index];
                numArray[index] = j;
            }
            this.buckets = numArray;
            this.entries = destinationArray;
        }


下个总结吧:凡是重复用可能为resize()的结构,比如dictionary、Arraylist,使用clear()的方法比较好。因为resize()很浪费时间
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值