先上代码,以1千万记录的内存查找测试:

 List<Student> stuList = new List<Student>();
Dictionary<int, Student> dictStu = new Dictionary<int, Student>();
Student student = null;
for (int i = ; i < ; i++)
{
student = new Student(i);
stuList.Add(student);
dictStu.Add(i, student);
}
Stopwatch sw = new Stopwatch();
sw.Start();
student = dictStu[];
sw.Stop();
Console.WriteLine(student.ToString());
Console.WriteLine("dict={0}", sw.ElapsedMilliseconds); sw.Reset();
sw.Start(); var yieldResult = StudentResult(stuList);
foreach (Student stu in yieldResult)
{
Console.WriteLine(stu.ToString());
}
// Student s = yieldResult.First<Student>();
//Console.WriteLine(s.ToString());
sw.Stop(); Console.WriteLine("yieldResult={0}", sw.ElapsedMilliseconds); sw.Reset();
sw.Start(); foreach (Student stu in stuList)
{
if (stu.Age == )
{
student = stu;
break;
}
} sw.Stop(); Console.WriteLine("foreach={0}", sw.ElapsedMilliseconds); sw.Reset();
sw.Start(); var result = from stu in stuList
where stu.Age ==
select stu; foreach (Student stu in result)
{
Console.WriteLine(stu.ToString());
}
sw.Stop(); Console.WriteLine("linq={0}", sw.ElapsedMilliseconds);
    static IEnumerable<Student> StudentResult(List<Student> stuList)
{
foreach (Student stu in stuList)
{
if (stu.Age == )
{
yield return stu;
} }
}

yield 查找方式的方法定义

执行结果

结论:

字典查找为哈希查找,性能最优,其次是foreach遍历,后依次为yield,linq

//var result = from stu in stuList
// //where stu.Age > 10 && stu.Age < 20
// select stu;

var result = stuList
.Where(r => r.Age > 10 && r.Age < 20)
.Select(r => r);

字典查找、linq、foreach、yield等几种查找性能对比的更多相关文章

  1. [笔记]Go语言写文件几种方式性能对比

    Go语言中写文件有多种方式,这里进行如下几种方式的速度对比: 打开文件,写入内容,关闭文件.如此重复多次 打开文件,写入内容,defer 关闭文件.如此重复多次 打开文件,重复多次写入内容,defer ...

  2. Elasticsearch的几种架构(ELK,EL,EF)性能对比测试报告

    Elasticsearch的几种架构性能对比测试报告 1.前言 选定了Elasticsearch作为存储的数据库,但是还需要对Elasticsearch的基础架构做一定测试,所以,将研究测试报告输出如 ...

  3. EF架构~linq模拟left join的两种写法,性能差之千里!

    回到目录 对于SQL左外连接我想没什么可说的,left join将左表数据都获出来,右表数据如果在左表中不存在,结果为NULL,而对于LINQ来说,要实现left join的效果,也是可以的,在进行j ...

  4. 【摩天大楼平地起】基础篇 09 简述N种查找算法

    引言 在开始之前首先可以先思考一下假如没有查找算法会是什么情况?所有数据结构都需要全部遍历一遍,每次都一遍又一遍的查,从本质而言查找算法就是为了提高效率. 经过前人一代又一代的努力,目前的查找算法大致 ...

  5. linux下五种查找命令

      我们经常需要在系统中查找一个文件或者命令,那么在Linux系统中如何快速定位和精确查找它呢?下面总结了五个基础命令·分别是which.whereis.type.locate.find. 一 whi ...

  6. ArrayList和LinkedList的几种循环遍历方式及性能对比分析(转)

    主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayList和LinkedList的源码实现分析性能结果,总结结论. 通过本文你可以 ...

  7. ArrayList和LinkedList的几种循环遍历方式及性能对比分析

    最新最准确内容建议直接访问原文:ArrayList和LinkedList的几种循环遍历方式及性能对比分析 主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性 ...

  8. ArrayList和LinkedList的几种循环遍历方式及性能对比分析(转载)

    原文地址: http://www.trinea.cn/android/arraylist-linkedlist-loop-performance/ 原文地址: http://www.trinea.cn ...

  9. 【转】ArrayList和LinkedList的几种循环遍历方式及性能对比分析

    原文网址:http://www.trinea.cn/android/arraylist-linkedlist-loop-performance/ 主要介绍ArrayList和LinkedList这两种 ...

随机推荐

  1. Jersey框架一:Jersey RESTful WebService框架简介

    Jersey系列文章: Jersey框架一:Jersey RESTful WebService框架简介 Jersey框架二:Jersey对JSON的支持 Jersey框架三:Jersey对HTTPS的 ...

  2. C语言常用的小代码

    圆周率Pi tan(Pi/4)=1 => Pi=4*arctan(1) 反正切函数arctan()在C语言里表示为atan(),为保证精度取圆周率的代码如下: const double Pi = ...

  3. ORA-27101:shared memory realm does not exist的问题

    ORA-27101:shared memory realm does not exist的问题 登陆SQLPlus时出现: ORA-01034:ORACLE not avaiable ORA-2710 ...

  4. MySQL加载并执行SQL脚本文件

    第一种方法: 命令行下(未连接数据库) ,输入 mysql -h localhost -u root -p123456 < C:\db.sql 第二种方法: 命令行下(已连接数据库,此时的提示符 ...

  5. Python Beautiful Soup模块的安装

    以安装Beautifulsoup4为例: 1.到网站上下载:http://www.crummy.com/software/BeautifulSoup/bs4/download/ 2.解压文件到C:\P ...

  6. framework 安装出错 1603

    安装frame work 3.5的时候老是出现 1603错误. 百度了一圈,各种方法都试了,仍不行. 像: 1.打开临时目录看安装日志,然后修改注册表Main的权限. 2.停止服务Cryptograp ...

  7. 深度优化LNMP

    优化前准备工作 Centos准备及配置 准备安装包及软件:http://pan.baidu.com/s/1chHQF  下载解压到U盘即可安装http://pan.baidu.com/s/15TUWf ...

  8. 黄聪:Discuz自制模板带jquery时与discuz本身冲突解决办法

    由于JQuery的效果很好,在制作模板时难免会用到各种jquery效果.可是做过模板的人就会发现加上自己的juery代码后,discuz自带的一些下拉功能就不可以使用了,其实原因就是discuz和JQ ...

  9. 开发高峰时的CPU使用率

  10. MyEclipse-File Serarch时报错:Problems encountered during text search