本文来自:http://www.cnblogs.com/353373440qq/p/3488367.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转换中的性能问题 转的更多相关文章

  1. List<T>与Dictionary<string,T>频繁检索的性能差距

    一直对LINQ简洁高效的语法青睐有加,对于经常和资料库,SQL语法打交道的C#开发者来说,LINQ无疑是一个非常不错的选择,当要在List<T>(T为一个普通对象)集合中查找满足某些条件的 ...

  2. 【翻译】.NET 5中的性能改进

    [翻译].NET 5中的性能改进 在.NET Core之前的版本中,其实已经在博客中介绍了在该版本中发现的重大性能改进. 从.NET Core 2.0到.NET Core 2.1到.NET Core ...

  3. 如何借助 OVN 来提高 OVS 在云计算环境中的性能

    众所周知,OpenvSwitch 以其丰富的功能和不错的性能,已经成为 Openstack 部署中最受欢迎的虚拟交换机.由于 Openstack Neutron 的架构引入了一些性能问题,比如 neu ...

  4. 【SQL系列】深入浅出数据仓库中SQL性能优化之Hive篇

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[SQL系列]深入浅出数据仓库中SQL性能优化之 ...

  5. 【译】ASP.NET Core 6 中的性能改进

    原文 | Brennan Conroy 翻译 | 郑子铭 受到 Stephen Toub 关于 .NET 性能的博文的启发,我们正在写一篇类似的文章来强调 6.0 中对 ASP.NET Core 所做 ...

  6. 优化Web中的性能

    优化Web中的性能 简介 web的优化就是一场阻止http请求最终访问到数据库的战争. 优化的方式就是加缓存,在各个节点加缓存. web请求的流程及节点 熟悉流程及节点,才能定位性能的问题.而且优化的 ...

  7. Ionic中使用Chart.js进行图表展示以及在iOS/Android中的性能差异

    Angular Chart 简介 在之前的文章中介绍了使用 Ionic 开发跨平台(iOS & Android)应用中遇到的一些问题的解决方案. 在更新0.1.3版本的过程中遇到了需要使用图表 ...

  8. 使用kettle转换中的JavaScript对密码进行加密和解密

    日常开发中,为了确保账号和密码的安全,时常要对密码进行加密和解密.然而kettle是怎么对密码进行加密和解密的呢? 下面的代码需要再转换中的JavaScript中运行. var encrypted_p ...

  9. 使用List,Dictionary加载数据库中的数据

    情景描述:数据库中有一张设备表,字段DWDM存放的是各个厂编号,字段ZNBH存放的是设备编号.其中DWDM跟ZNBH是一对多的关系.需要将数据库中的值加载到List<Dictionary< ...

随机推荐

  1. Minimum Depth of Binary Tree 解答

    Question Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along ...

  2. asp.net 错误处理

    一.从客户端(...)中检测到有潜在危险的 Request.Form 值.(如图) 解决办法: 1.为 c:/windows/temp 文件夹 设置 IIS_Iusers 可读写权限 (可解决部分问题 ...

  3. Scala--样例类(case)详解

    概述: case类在模式匹配和actor中经常使用到,当一个类被定义成为case类后,Scala会自动帮你创建一个伴生对象并帮你实现了一系列方法且带来了不少好处,如下: 1.实现了apply方法,意味 ...

  4. [bzoj2301: [HAOI2011]Problem b] 乞讨

    </pre><pre code_snippet_id="507886" snippet_file_name="blog_20141104_2_53831 ...

  5. Javascript基础学习笔记

    什么是变量?变量是用于存储信息的容器变量的声明语法: var 变量名 变量名 = 值;变量要先声明再赋值变量可以重复赋值变量的命名规则变量必须以字母开头:变量也能以$和_符号开头(不过我们不推荐这么做 ...

  6. ios ColorLUT滤镜

    通过这种方格图片实现滤镜 代码: "CIFilter+ColorLUT.h" "CIFilter+ColorLUT.m" #import "CIFil ...

  7. iOS 性能优化:Instruments

    对于每位 iOS 开发者来说,代码性能是个避不开的话题.随着项目的扩大和功能的增多,没经过认真调试和优化的代码,要么任性地卡顿运行,要么低调地崩溃了之……结果呢,大家用着不高兴,开发者也不开心. 其实 ...

  8. B - 敌兵布阵 线段树的点

    B - 敌兵布阵 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descriptio ...

  9. 从汇编看c++内联函数评估求值

    在c++中,一个inline函数实体,在整个class 声明未被完全看到之前,是不会被评估求值的,也就是说,对于类里面内联的成员函数本身的分析,要等到class的声明完全结束之后才开始.下面试c++源 ...

  10. MysqlDataSource里的Connection实现类实现了isValid(int timeout)方法

    在项目中,需要连接mysql数据库的时候,我们最好选择使用数据库连接池,即需要选择DataSource. 而在使用c3p0的ComboPooledDataSource时,发现它的Connection实 ...