Dictionary和数组查找效率对比

    今天想到前两天的通用数据库访问实体类中,想到了赋值和取值的效率,于是今天就写了个小程序测试了额一下

测试程序代码:

int Count = 1000000;
            string findName = "Field" + (Count-1).ToString();
            DataEntity[] _ArrTestStr = new DataEntity[Count];
            Dictionary<string, DataEntity> _DictionaryTest = new Dictionary<string, DataEntity>();
            StringBuilder content = new StringBuilder();
            DateTime beginDateTime = DateTime.Now;
            //对测试数据进行填充
            for (int i = 0; i < Count; i++)
            {
                DataEntity de = new DataEntity();
                de.Value = i.ToString();
                de.Field = "Field" + de.Value;
                _ArrTestStr[i] = de;
            }
            DateTime endDateTime = DateTime.Now;
            content.Append(" 数组赋值开始时间:").Append(beginDateTime.Ticks).AppendLine(beginDateTime.ToString("yyyy-MM-dd HH:mm:ss fff"))
                 .Append(" 数组赋值结束时间:").Append(beginDateTime.Ticks).AppendLine(beginDateTime.ToString("yyyy-MM-dd HH:mm:ss fff"));


            beginDateTime = DateTime.Now;
            for (int i = 0; i < Count; i++)
            {
                DataEntity de = new DataEntity();
                de.Value = i.ToString();
                de.Field = "Field" + de.Value;
                _DictionaryTest.Add(de.Field, de);
            }
            endDateTime = DateTime.Now;
            content.Append(" Dictionary赋值开始时间:").Append(beginDateTime.Ticks).AppendLine(beginDateTime.ToString("yyyy-MM-dd HH:mm:ss fff"))
                  .Append(" Dictionary赋值结束时间:").Append(beginDateTime.Ticks).AppendLine(beginDateTime.ToString("yyyy-MM-dd HH:mm:ss fff"));


            int index = 0;
            DataEntity NewDE = null;
            beginDateTime = DateTime.Now;
            while (index<Count)
            {
                if (_ArrTestStr[index].Field == findName)
                {
                    NewDE = _ArrTestStr[index];
                    break;
                }
                index++;
            }
            endDateTime = DateTime.Now;
           
            content.Append("数组查找到的值为:").AppendLine(NewDE.Value)
                .Append(" 开始查找时间:").Append(beginDateTime.Ticks).AppendLine(beginDateTime.ToString("yyyy-MM-dd HH:mm:ss fff"))
                 .Append(" 结束查找时间:").Append(beginDateTime.Ticks).AppendLine(beginDateTime.ToString("yyyy-MM-dd HH:mm:ss fff"));


            beginDateTime = DateTime.Now;
            NewDE = _DictionaryTest[findName];
            endDateTime = DateTime.Now;
            content.Append("Dictionary查找到的值为:").AppendLine(NewDE.Value)
               .Append(" 开始查找时间:").Append(beginDateTime.Ticks).AppendLine(beginDateTime.ToString("yyyy-MM-dd HH:mm:ss fff"))
                .Append(" 结束查找时间:").Append(beginDateTime.Ticks).AppendLine(beginDateTime.ToString("yyyy-MM-dd HH:mm:ss fff"));
            textBox1.Text = content.ToString();


程序运行结果:

 数组赋值开始时间:6348196539849141122012-08-30 23:16:38 491
 数组赋值结束时间:6348196539849141122012-08-30 23:16:38 491
 Dictionary赋值开始时间:634819653995374710 2012-08-30 23:16:39 537
 Dictionary赋值结束时间:6348196539953747102012-08-30 23:16:39 537
数组查找到的值为:999999
 开始查找时间:6348196540108655962012-08-30 23:16:41 086
 结束查找时间:6348196540108655962012-08-30 23:16:41 086
Dictionary查找到的值为:999999
 开始查找时间:6348196540112756192012-08-30 23:16:41 127
 结束查找时间:6348196540112756192012-08-30 23:16:41 127

 

 

结论:赋值和查找效率基本上是一样的,循环了10万次

转载于:https://www.cnblogs.com/shunhe316/archive/2012/08/30/2664667.html

你可能感兴趣的:(Dictionary和数组查找效率对比)