Dictionary到List转换中的性能问题
来源:互联网 发布:淘宝网口罩图片大全 编辑:程序博客网 时间:2024/06/08 13:45
出处:http://www.189works.com/article-40289-1.html
在应用泛型中,我们经常使用Dictionary,经常会用到Dictionary到List的转换。
经过各位高人指点后,做出适当调整,以免误人子弟,特此对关注此帖的同仁深表感谢。希望能继续提醒、斧正。
Dictionary转换为List通常方法,可以有五种:
1、创建List的时候,将Dictionary的Value值作为参数
2、创建List后,调用List.AddRange方法
3、建立List,循环Dictionary逐个赋值
4、通过Linq查询,得到结果后调用ToList方法
5、用Dictionary对象自带的ToList方法
但是五种方法如何取舍呢?性能方面哪种更好一点呢?
针对此疑问,特做了测试验证。
测试结果如下:(经过多次测试,取平均值)
/*测试结果(时间为毫秒) * * ============================================= * * 数据 | 10W | 100W | 1000W * * --------------------------------------------- * * 创建集合 | 2 | 28 | 280 * * --------------------------------------------- * * AddRange | 19 | 33 | 362 * * --------------------------------------------- * * 循环赋值 | 7 | 60 | 869 * * --------------------------------------------- * * Linq查询 | 8 | 7 | 238 此没有相关性,只是作为下面ToList方法的参考 * * --------------------------------------------- * * Linq查询 * * 后ToList | 11 | 97 | 1627 * * --------------------------------------------- * * ToList方法 | 5 | 23 | 948 * * * */
通过上述结果,可以得出结论:
结论1:方法1和方法2性能较好,可优先考虑方法1
结论2:TOList方法性能方面稍差,方法4和方法5不可取。
具体测试用例代码如下,如有纰漏,请指出:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace ToListTest
{
class Program
{
static void Main(string[] args)
{
TestToList();
}
/// <summary> /// 测试代码 /// </summary> private static void TestToList()
{
Dictionary<int, Person> dic = new Dictionary<int, Person>();
#region 填充数据
Person p;
for (int i = 0; i < 100000; i++)
{
p = new Person(i, "P_" + i.ToString());
dic.Add(i, p);
}
#endregion
List<Person> pList;
Stopwatch watcher = new Stopwatch();
#region 创建集合对象时,将集合作为参数
watcher.Reset();
watcher.Start();
pList = new List<Person>(dic.Values);
Console.WriteLine("new List<>\t{0}", watcher.ElapsedMilliseconds);
watcher.Stop();
#endregion
#region 调用方法AddRange
pList = new List<Person>();
watcher.Reset();
watcher.Start();
pList.AddRange(dic.Values);
Console.WriteLine("测试AddRange\t{0}", watcher.ElapsedMilliseconds);
watcher.Stop();
#endregion
#region 测试循环赋值
pList = new List<Person>();
watcher.Reset();
watcher.Start();
foreach (var item in dic)
{
pList.Add(item.Value);
}
Console.WriteLine("测试循环赋值\t{0}", watcher.ElapsedMilliseconds);
watcher.Stop();
#endregion
#region 测试Linq
watcher.Reset();
watcher.Start();
//var list = from temp in dic // select temp.Value; //pList = list as List<Person>; //pList 确实为Null,这样操作错误 //原因: //得到的list为:{System.Linq.Enumerable.WhereSelectEnumerableIterator<KeyValuePair<int,Person>,Person>}
IEnumerable<Person> list = from temp in dic
select temp.Value;
Console.WriteLine("测试Linq\t{0}", watcher.ElapsedMilliseconds);
watcher.Stop();
#endregion
#region 测试Linq 需要ToList()
watcher.Reset();
watcher.Start();
pList = (from temp in dic
select temp.Value).ToList();
Console.WriteLine("测试Linq,需要ToList\t{0}", watcher.ElapsedMilliseconds);
watcher.Stop();
#endregion
#region 测试ToList
watcher.Reset();
watcher.Start();
pList = dic.Values.ToList<Person>();
Console.WriteLine("测试ToList\t{0}", watcher.ElapsedMilliseconds);
watcher.Stop();
#endregion
/*测试结果(时间为毫秒) * * ============================================= * * 数据 | 10W | 100W | 1000W * * --------------------------------------------- * * 创建集合 | 2 | 28 | 280 * * --------------------------------------------- * * AddRange | 19 | 33 | 362 * * --------------------------------------------- * * 循环赋值 | 7 | 60 | 869 * * --------------------------------------------- * * Linq查询 | 8 | 7 | 238 此没有相关性,只是作为下面ToList方法的参考 * * --------------------------------------------- * * Linq查询 * * 后ToList | 11 | 97 | 1627 * * --------------------------------------------- * * ToList方法 | 5 | 23 | 948 * * * */
}
class Person
{
private int id;
public int ID
{
get { return id; }
set { id = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
public Person(int id, string name)
{
this.id = id;
this.name = name;
}
}
}
}
- Dictionary到List转换中的性能问题
- List、Dictionary转Dictionary的性能分析
- HashSet 、 Dictionary 、List性能对比
- C#HashSet 、 Dictionary 、List性能对比
- xml对象转换成Dictionary或者list
- list,string,tuple,dictionary之间的转换
- Python中的List,Tuple和Dictionary
- python中的list,tuple 和 dictionary
- 谈谈Dictionary和List的问题
- List和Dictionary的Contains方法性能比较
- c# 下的List和Dictionary性能比较
- ASP.NET中Dictionary如何转换为list
- List 性能问题
- 随机数的种子问题、泛型集合:list Dictionary、字典Dictionary【充实的一天~!】
- VC6 VS2003转换到VS2005中的一些问题
- 在Swift中的ASCII到字符转换的问题
- Dictionary List类使用
- Oracle Data Dictionary List
- C#封装RichTextBox实现鼠标停留,显示当前单词文本的tooltip提示
- POJ 3469 第一次AC总结(Dinic+多路增广)
- 分布式经典论文
- Linq to Entities in Ado.net EF的事务
- 第八周任务三实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、求反、比较(6种关系)的运算。
- Dictionary到List转换中的性能问题
- 【转]ubuntu 环境变量设置方法
- An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
- iPhone里的29个小技巧指南(上)
- CRichEditCtrl 使用技巧
- The APR based Apache Tomcat Native library which allows optimal performance in p
- 近期面试题汇总
- MapReduce: 一个巨大的倒退(zz)
- 数据表的连接