先上代码,以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. 【转】第5篇:Xilium CefGlue 关于 CLR Object 与 JS 交互类库封装报告:自动注册JS脚本+委托回调方法分析

    作者: 牛A与牛C之间 时间: 2013-11-19 分类: 技术文章 | 暂无评论 | 编辑文章 主页 » 技术文章 » 第5篇:Xilium CefGlue 关于 CLR Object 与 JS ...

  2. Python关键字yield的解释(stackoverflow)

    3.1. 提问者的问题 Python关键字yield的作用是什么?用来干什么的? 比如,我正在试图理解下面的代码: def node._get_child_candidates(self, dista ...

  3. ajax简单案例:返回json型

    主页: <!--输入代号点击查询查到本代号的人名--> <body> <div> 请输入代号:<input type="text" id= ...

  4. Intent传递数据从一个Activity到另一个Activity

    MainActivity package com.test.intentdemo; import android.app.Activity; import android.content.Intent ...

  5. TCP长连接与短连接

    1.概念区别 所谓TCP短连接,是指通信双方有数据交互时,就建立一个TCP连接,数据发送完成后,则断开此TCP连接.也就是说TCP连接维持的时间比较短.一般银行网页数据交互都使用短连接.再比如说htt ...

  6. 【linux】日志管理

    1.日志文件内容的一般格式 (1)事件发生的日期与时间: (2)发生此事件的主机名: (3)启动此事件的服务名称或函数名称: (4)该信息的实际数据内容. 例如:Mar 14 15:38:00 www ...

  7. 剑指offer系列22--二叉树中和为某一值的路径

    22[题目]输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径. * 路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径 * [思路]只要从根结点到叶结点一条一条遍 ...

  8. 【设计模式】单件模式(Singleton)--各类单件模式的比较

    单件模式 确保一个类只有一个实例,并提供一个安全的访问点. 线程安全+延时初始化+高性能(使用:延时初始化占位符模式) ------测试----------- 线程安全+[非]延时初始化 线程安全+延 ...

  9. 216. Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  10. TMS320C54x系列DSP的CPU与外设——第1章 绪论

    第1章 绪论 TMS320C54x DSP是TMS320系列DSP产品中的定点数字信号处理器.C54x DSP满足了实时嵌入式应用的一些要求,例如通信方面的应用. C54x的中央处理单元(CPU)具有 ...