[英]LINQ Count() until, is this more efficient?
假设我想检查集合中是否至少有N个元素。
这比做好吗?
Count() >= N
使用:
public static bool AtLeast<T>(this IEnumerable<T> enumerable, int max)
{
int count = 0;
return enumerable.Any(item => ++count >= max);
}
甚至
public static bool Equals<T>(this IEnumerable<T> enumerable, int amount)
{
return enumerable.Take(amount).Count() == amount;
}
我怎么能对此进行基准测试
/// <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;
}
在.Count()
方法中内置了一些记录良好的优化。 具体来说,如果你的枚举是一个ICollection
, .Count()
将是一个常量操作,因为它将使用ICollection
的.Count
属性。
但是,在一般情况下,它将迭代整个IEnumerable
以获取计数。 如果你没有ICollection
,当你有超过N个元素时,你最好使用两种建议的方法。 对于这两者的相对表现,你必须像其他人建议的那样对它们进行分析。
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);
PrintTime()打印出计时器滴答的结果:
True 20818
True 8774
True 8688
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.