今天想到前两天的通用数据库访问实体类中,想到了赋值和取值的效率,于是今天就写了个小程序测试了额一下
测试程序代码:
int Count = 1000000; string findName = "Field" + (Count-1).ToString(); DataEntity[] _ArrTestStr = new DataEntity[Count]; Dictionary_DictionaryTest = new Dictionary (); 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
数组赋值开始时间: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 086Dictionary查找到的值为:999999 开始查找时间:6348196540112756192012-08-30 23:16:41 127 结束查找时间:6348196540112756192012-08-30 23:16:41 127
结论:赋值和查找效率基本上是一样的,循环了10万次