如果我们看一下扩展方法的源代码,Any我们将se,它总是使用枚举器:
public static bool Any<TSource>(this IEnumerable<TSource> source)
{
if (source == null)
throw Error.ArgumentNull(nameof (source));
using (IEnumerator<TSource> enumerator = source.GetEnumerator())
{
if (enumerator.MoveNext())
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
我认为,Count如果集合IList就像在SingleOrDefault方法中那样检查属性是不是更好(对于perfomace):
public static TSource SingleOrDefault<TSource>(this IEnumerable<TSource> source)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
IList<TSource> sourceList = source as IList<TSource>;
if (sourceList != null)
{
switch (sourceList.Count)
{
case 0:
return default(TSource);
case 1:
return sourceList[0];
}
}
else
{
//...
}
throw Error.MoreThanOneElement();
}
Run Code Online (Sandbox Code Playgroud)
我说,它看起来像这样:
private static bool Any<TSource>(IEnumerable<TSource> source)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
IList<TSource> sourceList = source as IList<TSource>;
if (sourceList != null)
{
return sourceList.Count != 0;
}
using (IEnumerator<TSource> enumerator = source.GetEnumerator())
{
if (enumerator.MoveNext())
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
我写了一个基准来测试它:
namespace AnyTests
{
class Program
{
static void Main(string[] args)
{
BenchmarkRunner.Run<Test>();
}
}
public class Test
{
private readonly List<int> list1 = new List<int>(new[] { 1, 2, 3, 4, 5 });
private readonly IEnumerable<int> list2 = GetCollection();
private static IEnumerable<int> GetCollection()
{
yield return 1;
}
[Benchmark]
public void TestLinqAnyList()
{
Enumerable.Any(list1);
}
[Benchmark]
public void TestNewAnyList()
{
NewAny(list1);
}
[Benchmark]
public void TestLinqAnyEnumerable()
{
Enumerable.Any(list2);
}
[Benchmark]
public void TestNewAnyEnumerable()
{
NewAny(list2);
}
private static bool NewAny<TSource>(IEnumerable<TSource> source)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
IList<TSource> sourceList = source as IList<TSource>;
if (sourceList != null)
{
return sourceList.Count != 0;
}
using (IEnumerator<TSource> enumerator = source.GetEnumerator())
{
if (enumerator.MoveNext())
return true;
}
return false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果显示它好大约两倍:
// * Summary *
BenchmarkDotNet=v0.10.13, OS=Windows 10 Redstone 3 [1709, Fall Creators Update] (10.0.16299.192)
Intel Core i7-7700 CPU 3.60GHz (Kaby Lake), 1 CPU, 8 logical cores and 4 physical cores
Frequency=3515624 Hz, Resolution=284.4445 ns, Timer=TSC
[Host] : .NET Framework 4.7.1 (CLR 4.0.30319.42000), 32bit LegacyJIT-v4.7.2600.0
DefaultJob : .NET Framework 4.7.1 (CLR 4.0.30319.42000), 32bit LegacyJIT-v4.7.2600.0
Method | Mean | Error | StdDev |
---------------------- |---------:|----------:|----------:|
TestLinqAnyList | 26.80 ns | 0.1382 ns | 0.1154 ns |
TestNewAnyList | 12.75 ns | 0.0480 ns | 0.0426 ns |
TestLinqAnyEnumerable | 18.03 ns | 0.0947 ns | 0.0886 ns |
TestNewAnyEnumerable | 23.51 ns | 0.0913 ns | 0.0762 ns |
Run Code Online (Sandbox Code Playgroud)
因为IList它大约好两倍,因为IEnumerable它差了大约20%.
那么,问题是:在SingleOrDefault方法中使用优化的原因是什么,不在Any一个中使用它?
您的问题背后的假设可能是:
算很快,为什么不用呢?
一个合理的答案为什么Any不使用它Count是不总是快.他们选择的实施的优点是它将具有相对稳定和低成本(即大致O(1)).然而,它可能并不像Count所有情况那样快(正如您所确定的那样).
无法保证实现IList或ICollection将具有快速 Count属性的类.ConcurrentDictionary例如,通常Count > 0比现有Any实现慢.
此外,您IList使用ICollection的代码可能会使用,因为您的代码不需要IList提供访问权限的额外功能.
| 归档时间: |
|
| 查看次数: |
241 次 |
| 最近记录: |