Dictionary 效能測試 Dictionary 效能測試
  .NET       ez      2012-06-04

首先測試索引查詢及內容查詢,所需的時間差距。

using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Test
{
    class Program
    {
        static Dictionary<string, string> D = new Dictionary<string, string>();

        /// <summary>
        /// 產生測試資料
        /// </summary>
        static void testdata()
        {
            int num = 1000000, x = 0;
            D.Clear();
            Stopwatch sw = new Stopwatch();
            sw.Start();
            for (int i = 0; i < num; i ++)
            {
                x++;
                D.Add((i * 2).ToString(), (i * 2).ToString());
            }
            sw.Stop();
            Console.WriteLine("產生資料" + num + "筆\t時間:" + sw.ElapsedMilliseconds.ToString());
        }

        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();

            //產生測試資料
            testdata();

            //搜尋索引值
            {
                sw.Reset();
                int num = 1000000;
                sw.Start();
                for (int i = 0; i < num; i++)
                {
                    if (D.ContainsKey(i.ToString())) { }
                }
                sw.Stop();
                Console.WriteLine("搜尋索引" + num + "筆\t時間:" + sw.ElapsedMilliseconds.ToString());
            }            

            //搜尋內容
            {
                sw.Reset();
                int num = 1000;
                sw.Start();
                for (int i = 0; i < num; i++)
                {
                    if (D.ContainsValue(i.ToString())) { }
                }
                sw.Stop();
                Console.WriteLine("搜尋內容" + num + "筆\t\t時間:" + sw.ElapsedMilliseconds.ToString());
            }

            Console.ReadLine();
        }
    }
}

可以發現,搜尋索引 及 內容查詢 差距非常的大,所以建議非必要不要使用內容查詢!

接下來測試,當遇到新增項目需求時,利用:

  1. 直接新增項目,加上try catch排除例外錯誤
  2. 利用ContainsKey先判斷是否存在,再新增項目
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Test
{
    class Program
    {
        static Dictionary<string, string> D = new Dictionary<string, string>();

        /// <summary>
        /// 產生測試資料
        /// </summary>
        static void testdata()
        {
            int num = 1000000, x = 0;
            D.Clear();
            Stopwatch sw = new Stopwatch();
            sw.Start();
            for (int i = 0; i < num; i ++)
            {
                x++;
                D.Add((i * 2).ToString(), (i * 2).ToString());
            }
            sw.Stop();
            Console.WriteLine("產生資料" + num + "筆\t時間:" + sw.ElapsedMilliseconds.ToString());
        }

        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            //產生測試資料
            testdata();
            //未判斷是否存在索引,直接新增索引
            {
                sw.Reset();
                int num = 10000;
                sw.Start();
                for (int i = 0; i < num; i++)
                {
                    try
                    {
                        D.Add(i.ToString(), i.ToString());
                    }
                    catch { }
                }
                sw.Stop();
                Console.WriteLine("未判斷是否存在索引,直接新增索引" + num + "筆\t時間:" + sw.ElapsedMilliseconds.ToString());
            }
            Console.WriteLine("\r\n\r\n");

            //產生測試資料
            testdata();
            //先判斷是否存在索引,再新增索引
            {
                sw.Reset();
                int num = 10000;
                sw.Start();
                for (int i = 0; i < num; i++)
                {
                    if (!D.ContainsKey(i.ToString())) D.Add(i.ToString(), i.ToString());
                }
                sw.Stop();
                Console.WriteLine("先判斷是否存在索引,再新增索引" + num + "筆\t時間:" + sw.ElapsedMilliseconds.ToString());
            }
            Console.WriteLine("\r\n\r\n");

            Console.ReadLine();
        }
    }
}

可以發現,使用ContainsKey優於直接新增方法,所以以後記得先判斷是否存在再新增項目!


標籤:   .NET

我要留言