字典查找、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这两种 ...
随机推荐
- unity, SerializedObject.FindProperty不要写在Editor的OnEnable里,要写在OnInspectorGUI里
如果像下面这样写: using UnityEngine;using System.Collections;using UnityEditor;using System.Collections.Gene ...
- Visual Studio 不生成.vshost.exe和.pdb文件的方法
使用Visual Studio编译工程时,默认设置下,即使选择了「Release」时也会生成扩展名为「.vshost.exe」和「.pdb」的文件. 一.先解释一下各个文件的作用: .pdb文件: 程 ...
- spring mvc 利用匿名内部类构建返回json对象
@RequestMapping(value = "/order/findOrderByIdVague/{noId}.json", method = {RequestMethod.G ...
- 概要设计、详细设计(三)关键点(Know-How)、运用技巧
1. 关键点(Know-How).运用技巧 4.1 设计准则 制定设计准则是概要设计阶段的最主要.最关键的工作.在实际工作中往往忽略,多数项目牺牲在这个环节.制定设计准则着眼于如何更好的做设计, ...
- jquery 事件绑定以及解绑定
var targetSelect = $("#@(Perfix)tbData tbody tr select[data-target]"); targetSelect.off(&q ...
- 【linux】 /etc/shadow 文件
格式:username: passwd: lastchg: min: max: warn: inactive: expire: flag 登录名:加密口令:最后一次修改时间:最小时间间隔:最大时间间隔 ...
- SSH_框架整合4--添加员工信息
SSH_框架整合4--添加员工信息 一. 1 index.jsp:添加:<a href="emp-input">添加员工向信息:Add Employees' Infor ...
- Eclipse高效率开发技巧
工欲善其事,必先利其器.对于程序员来说,Eclipse便是其中的一个"器".本文会从Eclipse快捷键和实用技巧这两个篇章展开介绍.Eclipse快捷键用熟后,不用鼠标,便可进行 ...
- Linux下文件的压缩和解压
tar命令 解包:tar zxvf FileName.tar 打包:tar czvf FileName.tar DirName gz命令 解压1:gunzip FileName.gz 解压2:gzip ...
- 关于学习Perl
Perl是一门很有用的语言,可以用它来做很多事.然而,它也仅是一门语言,掌握了Perl,你只是掌握了Computer领域的一小块知识.在学习Perl前,请明确你的学习目的,并采用正确的学习方法和资源. ...