字典查找、linq、foreach、yield等几种查找性能对比
先上代码,以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等几种查找性能对比的更多相关文章
- [笔记]Go语言写文件几种方式性能对比
Go语言中写文件有多种方式,这里进行如下几种方式的速度对比: 打开文件,写入内容,关闭文件.如此重复多次 打开文件,写入内容,defer 关闭文件.如此重复多次 打开文件,重复多次写入内容,defer ...
- Elasticsearch的几种架构(ELK,EL,EF)性能对比测试报告
Elasticsearch的几种架构性能对比测试报告 1.前言 选定了Elasticsearch作为存储的数据库,但是还需要对Elasticsearch的基础架构做一定测试,所以,将研究测试报告输出如 ...
- EF架构~linq模拟left join的两种写法,性能差之千里!
回到目录 对于SQL左外连接我想没什么可说的,left join将左表数据都获出来,右表数据如果在左表中不存在,结果为NULL,而对于LINQ来说,要实现left join的效果,也是可以的,在进行j ...
- 【摩天大楼平地起】基础篇 09 简述N种查找算法
引言 在开始之前首先可以先思考一下假如没有查找算法会是什么情况?所有数据结构都需要全部遍历一遍,每次都一遍又一遍的查,从本质而言查找算法就是为了提高效率. 经过前人一代又一代的努力,目前的查找算法大致 ...
- linux下五种查找命令
我们经常需要在系统中查找一个文件或者命令,那么在Linux系统中如何快速定位和精确查找它呢?下面总结了五个基础命令·分别是which.whereis.type.locate.find. 一 whi ...
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析(转)
主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayList和LinkedList的源码实现分析性能结果,总结结论. 通过本文你可以 ...
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析
最新最准确内容建议直接访问原文:ArrayList和LinkedList的几种循环遍历方式及性能对比分析 主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性 ...
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析(转载)
原文地址: http://www.trinea.cn/android/arraylist-linkedlist-loop-performance/ 原文地址: http://www.trinea.cn ...
- 【转】ArrayList和LinkedList的几种循环遍历方式及性能对比分析
原文网址:http://www.trinea.cn/android/arraylist-linkedlist-loop-performance/ 主要介绍ArrayList和LinkedList这两种 ...
随机推荐
- alpha预乘
将(r,g,b,a)变为(r*a,g*a,b*a,a)的操作称为alpha预乘. 对于alpha预乘的图片,应使用(One,OneMinusSrcAlpha)进行混合. 使用alpha预乘方式混合出来 ...
- JSP页面中<%! %>和<% %>的区别
JSP声明语句:<%!声明语句%>,通常声明全局变量.常量.方法.类JSP Scriptlet:<%java代码%>,其中可包含局部变量.java语句JSP表达式:<%= ...
- 数据接口管理工具 thx RAP
RAP是数据接口管理工具.在开发时前端将请求转至RAP,由RAP提供模拟数据:而后端使用RAP测试接口的正确性.这样RAP就成为了开发过程中的强 依赖,进而确保接口文档的实时正确性.RAP采用JSON ...
- AJAX状态值与状态码
在<Pragmatic Ajax A Web 2.0 Primer > 0: (Uninitialized) the send( ) method has not yet been inv ...
- redis 服务访问密码设定
1. 更改redis.conf配置 # requirepass foobared 去掉注释,foobared改为 自己的password , 我测试的时候用的是 redis-password 2.启动 ...
- UI-动画
// ------------------UIImageView的动画------------------ // ------------------UIView的动画---------------- ...
- HackerRank "No Prefix Set"
Typical Trie usage. But please note that it could be any order of input strings. #include <algori ...
- 【MySQL】数据行长度的一些限制
今天开发在导入数据的时候报一个错误: Row size too large. The maximum row size for the used table type, not counting BL ...
- shell下root用户切换其他用户运行程序
工作中,一些程序,需要随机启动,但是不是以root用户运行,于是需要在rc.local中通过shell,从root用户切换到其他用户运行程序,命令如下: su -c 'command' - user ...
- WLS_Oracle Weblogic安装和环境搭建(案例)
2014-01-03 Created By BaoXinjian