本文来自: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. Perl Symbolic Reference

    看一些模块的代码,很多时候通过*glob的方式来改变变量或者函数,这种方法称为Symbolic reference. 首先看一下*glob的结构,这个在之前的博文已经讲过,不做细述: SV = PVG ...

  2. (转)Java 的swing.GroupLayout布局管理器的使用方法和实例

    摘自http://www.cnblogs.com/lionden/archive/2012/12/11/grouplayout.html (转)Java 的swing.GroupLayout布局管理器 ...

  3. linux内核中驱动开发常见的相似多态

    #include<stdio.h> #include<stdlib.h> struct test { char name[20]; void (*func)(char *); ...

  4. 《Qt编程的艺术》——5.1 手动布局

    在传统的GUI设计中,每个控件(Widget)都要手动地绑定在窗口之上的一个点上(也就是说,这个控件被指定成了给定GUI元素的父对象),同时还要指定这个控件的高度和宽度.作为所有图形元素的基础类,QW ...

  5. zTree实现清空选中的第一个节点的子节点

    zTree实现清空选中的第一个节点的子节点 1.实现源代码 <!DOCTYPE html> <html> <head> <title>zTree实现基本 ...

  6. hdu 4750 Count The Pairs (2013南京网络赛)

    n个点m条无向边的图,对于q个询问,每次查询点对间最小瓶颈路 >=f 的点对有多少. 最小瓶颈路显然在kruskal求得的MST上.而输入保证所有边权唯一,也就是说f[i][j]肯定唯一了. 拿 ...

  7. Android实战技巧之十九:android studio导出jar包(Module)并获得手机信息

    AS中并没有独立的Module 工程,可是能够在普通的Project中增加Module.所谓的Module就是我们通常所指的模块化的一个单元.并经常以jar包的形式存在.以下以一个获取手机信息的样例演 ...

  8. 使用AsyncTask实现图片加载

    如上图所示:我们看到的就是使用PrograssDialog进度条和AsyncTask异步任务实现的效果(额,不要看应用名...).下面介绍一下具体的实现流程. 一.首先使用XML布局,布局很简单直接上 ...

  9. MVC 全局异常过滤器HandleErrorAttribute

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  10. eclipse使用Git插件

    折腾了会Git,记录一下下. 1.安装Git  Help-->Install New Software  点击Add,Name随意,Location为http://download.eclips ...