1、count和any

今天看了鹤冲天的文章:Linq:切勿使用 Count() > 0 来判断集合非空   有所收获,写下文章总结一下:

先看如下代码:

        static void Main(string[] args)
{
Stopwatch watch = new Stopwatch();
watch.Start();
var nums = GetNums(, );
SomeAction(nums);
watch.Stop();
Console.WriteLine(watch.ElapsedMilliseconds);
Console.WriteLine("over");
Console.ReadKey();
}
public static IEnumerable<int> GetNums(int start, int count)
{
var end = start + count;
for (int i = start; i < end; i++)
yield return i;
}
public static void SomeAction<T>(IEnumerable<T> source)
{
//if (source.Count() > 0) //2233 毫秒
if (source.Any()) //3 毫秒
{
Console.WriteLine("have data");
}
}

自己调试了下,当yield循环低于100000次的话,count()和Any()的执行时间差不多,count()时间略大于any()所耗费的时间,当大于100000次的话,count()执行时间就远比Any()执行时间多很多!例如上面的程序执行结果,count耗时2233毫秒,any却只耗时3毫秒!这是为什么呢?我们来看下原因:分别反编译 Enumerate.Count 的源码和 Enumerate.Any的源码,如下:

Enumerate.Any 实现代码如下:

 public static bool Any<TSource>(this IEnumerable<TSource> source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
using (IEnumerator<TSource> enumerator = source.GetEnumerator())
{
if (enumerator.MoveNext())
{
return true;
}
}
return false;
}

Enumerate.Count 的源码如下:

 public static int Count<TSource>(this IEnumerable<TSource> source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
ICollection<TSource> is2 = source as ICollection<TSource>;
if (is2 != null)
{
return is2.Count;
}
ICollection is3 = source as ICollection;
if (is3 != null)
{
return is3.Count;
}
int num = ;
using (IEnumerator<TSource> enumerator = source.GetEnumerator())
{
while (enumerator.MoveNext())
{
num++;
}
}
return num;
}

无论循环多少次,Any方法执行的时间总是最少的,大概10毫秒之内,我想大家从Any的源码可以知道这个原因,Any方法先判断集合是否为空,然后判断集合是否有数据,只进行一次movenext(),并未进行循环查询集合数量!所以耗费的时间当然一直是最少的了!

我们再看Count方法,同样是先进行集合判空操作,然后判断当前的集合source是否能转成ICollection类型,如果能转成,就直接返回此集合的Count属性,不会在进行下面的循环获取集合个数的操作了,为什么Count()方法比Any()方法执行的时间长呢?答案就在于此:因为GetNums方法中用yield返回纯粹的IEnumerable<int>类型,但是ICollection<T>是继承自IEnumerable<T>类型的,所以必然不好被转换,也就是Count()源码中的下面代码不会执行

     ICollection is3 = source as ICollection;
if (is3 != null)
{
return is3.Count;
}

上面的代码不会执行,那么必然会进行下面的代码操作,也就是循环获取集合的个数:

     int num = ;
using (IEnumerator<TSource> enumerator = source.GetEnumerator())
{
while (enumerator.MoveNext())
{
num++;
}
}
return num;

这样必然会导致Count()方法耗时很久!

2、如果我们把GetNums进行如下修改,其他代码不动:

       public static IEnumerable<int> GetNums(int start, int count)
{
List<int> list = new List<int>();
var end = start + count;
for (int i = start; i < end; i++)
list.Add(i);
return list;
}

这时我们在执行程序,可以看到无论循环次数多少,Count和Any两者执行的时间差不多,大概是1315毫秒左右,其中1312毫秒用于list集合的装填工作,3毫秒用于Count或Any的判断时间,为什么会出现这种情况呢,我想通过上面大家都知道了,因为此时GetNums方法返回的是List<T>类型,此类型继承于ICollection,所以必然可以被转换,就会返回此类型的Count属性,Count 方法对 ICollection 进行了优化,直接访返回它的 Count 属性,也就是返回一个数,当然很快了,下面的循环获取集合的个数当然也就不会执行了,也就节约了时间。

3、对某人问题的思考

有人说:”我有个程序里经常要判集合里是否仅有一个元素,又不能用Count,不得已自己写了个扩展方法 bool Check(this IEnumerable<T> source, int n), 当读到第n+1个元素就直接返回false“

我想如果此人用的集合是继承于ICollection<T>的话,例如List<T>,那么是没必要对IEnumerable<T>进行扩展的,通过反编译Count源码可以知道,如果集合source可以转换成ICollection的话,是可以直接通过count属性获取到集合的总数量的,所以耗费的时间要不了多少,也就没必要对IEnumerable进行扩展了!反之,如果不是继承ICollection<T>的话,例如IEnumerable,就必须自己扩展IEnumerable方法了,如果还用count的话,就会造成循环了,也就降低了程序的运行效率了!

4、参考

http://www.cnblogs.com/ldp615/archive/2011/12/11/2284154.html#comment_tip  鹤冲天

作者:MrZivChu

2013-12-13   21:30:12

Linq中Count()和Any()引发的效率问题的更多相关文章

  1. MVC+Spring.NET+NHibernate .NET SSH框架整合 C# 委托异步 和 async /await 两种实现的异步 如何消除点击按钮时周围出现的白线? Linq中 AsQueryable(), AsEnumerable()和ToList()的区别和用法

    MVC+Spring.NET+NHibernate .NET SSH框架整合   在JAVA中,SSH框架可谓是无人不晓,就和.NET中的MVC框架一样普及.作为一个初学者,可以感受到.NET出了MV ...

  2. Linq中关键字的作用及用法

    Linq中关键字的作用及用法 1.All:确定序列中的所有元素是否都满足条件.如果源序列中的每个元素都通过指定谓词中的测试,或者序列为空,则为 true:否则为 false. Demo: 此示例使用 ...

  3. Linq 中按照多个值进行分组(GroupBy)

    Linq 中按照多个值进行分组(GroupBy) .GroupBy(x => new { x.Age, x.Sex }) group emp by new { emp.Age, emp.Sex ...

  4. SQL语句中count(1)count(*)count(字段)用法的区别

    SQL语句中count(1)count(*)count(字段)用法的区别 在SQL语句中count函数是最常用的函数之一,count函数是用来统计表中记录数的一个函数, 一. count(1)和cou ...

  5. linq中AsEnumerable和AsQueryable的区别

    本文导读:用Linq来操作集合的时候会用到AsQueryable()和AsEnumerable(),何时该用AsQueryable()和何时该用AsEnumerable(),或许存在些疑惑.AsQue ...

  6. Linq中使用Left Join

    use Test Create table Student( ID ,) primary key, ) not null ) Create Table Book( ID ,) primary key, ...

  7. 简述Linq中.ToList(), .AsEnumerable(), AsQueryable()的区别和用法

    [TOC] 这3个方法的功能完全不同, 应按照具体业务场景使用. AsQueryable() 先说说什么是 IQueryable IQueryable 是当前的 data provider 返回的类型 ...

  8. linq中group by

    本文导读:LINQ定义了大约40个查询操作符,如select.from.in.where.group 以及order by,借助于LINQ技术,我们可以使用一种类似SQL的语法来查询任何形式的数据.L ...

  9. Linq中 AsQueryable(), AsEnumerable()和ToList()的区别和用法

    Linq中 AsQueryable(), AsEnumerable()和ToList()的区别和用法:在写LINQ语句的时候,往往会看到AsEnumerable() ,AsQueryable() 和T ...

随机推荐

  1. Kruskal算法求最小生成树(POJ2485)

    题目链接:http://poj.org/problem?id=2485 #include <iostream> #include <stdio.h> #include < ...

  2. 20145238-荆玉茗 《Java程序设计》第7周学习总结

    20145238 <Java程序设计>第7周学习总结 教材学习内容总结 第13章时间与日期 13.1.1 ·即使标注为GMT(格林威治时间),实际上谈到的的是UTC(Unix时间)时间. ...

  3. window下绝对路径

    项目中配置文件(properties或yml)和项目是分离的,常见的配置方法如下: <profiles> <profile> <id>mas</id> ...

  4. PNChart,简洁高效有动画效果的iOS图表库

    导入 pod导入相对简单,要手动导入这个库,先下载下来(https://github.com/kevinzhow/PNChart),解压后把PNChart文件夹拖入工程中 运行发现#import&qu ...

  5. http状态码有那些,分别代表什么意思

    http1.0和2.0的区别https://blog.csdn.net/linsongbin1/article/details/54980801/ 简单版:         100  Continue ...

  6. 【杂题总汇】Codeforces-67A Partial Teacher

    [Codeforces-67A]Partial Teacher 上周刷了一大堆小紫薯的动态规划的题

  7. 【杂题总汇】UVa-10618 Tango Tango Insurrection

    [UVa-10618] Tango Tango Insurrection ◇ 题目 +vjudge 链接+ (以下选自<算法竞赛入门经典>-刘汝佳,有删改) <题目描述> 你想 ...

  8. Maven - 依赖范围<scope></scope>

    6种:

  9. C#基础-面向对象-封装

    封装 命名空间 using System; using System.Collections.Generic; using System.Text; namespace ConsoleApp6 { c ...

  10. java数组之二分法查找

    认识: 猜字游戏 步数 所猜的数 结果 可能值的范围 0     1~100 1 50 太高 1~49 2 25 太低 26~49 3 37 太高 26~36 4 31 太低 32~36 5 34 太 ...