[英]LINQ Count() until, is this more efficient?
Say I want to check whether there are at least N elements in a collection. 假设我想检查集合中是否至少有N个元素。
Is this better than doing? 这比做好吗?
Count() >= N
Using: 使用:
public static bool AtLeast<T>(this IEnumerable<T> enumerable, int max)
{
int count = 0;
return enumerable.Any(item => ++count >= max);
}
Or even 甚至
public static bool Equals<T>(this IEnumerable<T> enumerable, int amount)
{
return enumerable.Take(amount).Count() == amount;
}
How could I benchmark this? 我怎么能对此进行基准测试
/// <summary>
/// Returns whether the enumerable has at least the provided amount of elements.
/// </summary>
public static bool HasAtLeast<T>(this IEnumerable<T> enumerable, int amount)
{
return enumerable.Take(amount).Count() == amount;
}
/// <summary>
/// Returns whether the enumerable has at most the provided amount of elements.
/// </summary>
public static bool HasAtMost<T>(this IEnumerable<T> enumerable, int amount)
{
return enumerable.Take(amount + 1).Count() <= amount;
}
There are some well-documented optimizations built in to the .Count()
method. 在
.Count()
方法中内置了一些记录良好的优化。 Specifically, if your enumerable is an ICollection
, .Count()
will be a constant-time operation as it will use the ICollection
's .Count
property. 具体来说,如果你的枚举是一个
ICollection
, .Count()
将是一个常量操作,因为它将使用ICollection
的.Count
属性。
However, in the general case it will iterate the entire IEnumerable
to get the count. 但是,在一般情况下,它将迭代整个
IEnumerable
以获取计数。 If you do not have an ICollection
, you'd be better off using either of your two suggested methods when there are more than N elements. 如果你没有
ICollection
,当你有超过N个元素时,你最好使用两种建议的方法。 For the relative performance of those two, you'd have to profile them as others have suggested. 对于这两者的相对表现,你必须像其他人建议的那样对它们进行分析。
var list = Enumerable.Range(1, 1000000);
var t = new Stopwatch();
t.Restart();
var count = list.Count() > 100000;
t.Stop();
PrintTime(t, count);
t.Restart();
var atLeast = list.AtLeast(100000);
t.Stop();
PrintTime(t, atLeast);
t.Restart();
var equals = list.Equalss(100000);
t.Stop();
PrintTime(t, equals);
Results where PrintTime() prints out the timer ticks: PrintTime()打印出计时器滴答的结果:
True 20818
True 8774
True 8688
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.