程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#:Dictionary<key,key>的Clear到底有多多塊

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

編輯:C#入門知識

有時我們希望復用一個Dictionary的時候,可以Clear()也可以,直接new一個新的對象,

兩者到底是哪個劃算呢?

我們看程序:

  private void Experiment()
        {

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

            //Dictionary aa = new Dictionary();

            for (var loop = 0; loop < 30000; loop++)
            {
                Dictionary aa = new Dictionary();
              
                
                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[] destinationArray = new Entry[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()很浪費時間

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved