روش بهینهی بررسی خالی بودن مجموعهها و آرایهها در NET 5.0.
نویسنده: وحید نصیری
تاریخ: ۱۳۹۹/۱۱/۲۸ ۱۵:۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
private IList<int>? _idsList;
private IEnumerable<int>? _idsEnumerable;
private int[]? _idsArray;
[GlobalSetup]
public void Setup()
{
_idsEnumerable = Enumerable.Range(0, 10000);
_idsList = _idsEnumerable.ToList();
_idsArray = _idsEnumerable.ToArray();
} var list = _idsList ?? new List<int>();
if (list.Any() is false) { } if (_idsList is null || _idsList.Any() is false) { } if (_idsList == null || _idsList.Any() is false) { } if (_idsList?.Any() is false) { } if (_idsList is { Count: > 0 } is false) { } if (_idsList?.Count == 0) { } if (_idsList == null || _idsList.Count == 0) { } <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
</ItemGroup>
</Project> using BenchmarkDotNet.Running;
namespace AnyCountBenchmark
{
public static class Program
{
static void Main(string[] args)
{
#if DEBUG
System.Console.WriteLine("Please set the project's configuration to Release mode first.");
#else
BenchmarkRunner.Run<Scenarios>();
#endif
}
}
} using System;
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Order;
namespace AnyCountBenchmark
{
[SimpleJob(RuntimeMoniker.NetCoreApp50)]
[Orderer(SummaryOrderPolicy.FastestToSlowest, MethodOrderPolicy.Declared)]
[RankColumn]
public class Scenarios
{
private IList<int>? _idsList;
private IEnumerable<int>? _idsEnumerable;
private int[]? _idsArray;
[GlobalSetup]
public void Setup()
{
_idsEnumerable = Enumerable.Range(0, 10000);
_idsList = _idsEnumerable.ToList();
_idsArray = _idsEnumerable.ToArray();
}
#region Any_With_Null_coalescing
[Benchmark]
public void List_Any_With_Null_coalescing()
{
var list = _idsList ?? new List<int>();
if (list.Any() is false) { }
}
[Benchmark]
public void Array_Any_With_Null_coalescing()
{
var array = _idsArray ?? Array.Empty<int>();
if (array.Any() is false) { }
}
[Benchmark]
public void Enumerable_Any_With_Null_coalescing()
{
var enumerable = _idsEnumerable ?? Enumerable.Empty<int>();
if (enumerable.Any() is false) { }
}
#endregion
#region Any_With_Is_Null_Check
[Benchmark]
public void List_Any_With_Is_Null_Check()
{
if (_idsList is null || _idsList.Any() is false) { }
}
[Benchmark]
public void Array_Any_With_Is_Null_Check()
{
if (_idsArray is null || _idsArray.Any() is false) { }
}
[Benchmark]
public void Enumerable_Any_With_Is_Null_Check()
{
if (_idsEnumerable is null || _idsEnumerable.Any() is false) { }
}
#endregion
#region Any_Any_With_Null_Equality_Check
[Benchmark]
public void List_Any_With_Null_Equality_Check()
{
if (_idsList == null || _idsList.Any() is false) { }
}
[Benchmark]
public void Array_Any_With_Null_Equality_Check()
{
if (_idsArray == null || _idsArray.Any() is false) { }
}
[Benchmark]
public void Enumerable_Any_With_Null_Equality_Check()
{
if (_idsEnumerable == null || _idsEnumerable.Any() is false) { }
}
#endregion
#region Any_With_Null_Conditional_Operator
[Benchmark]
public void List_Any_With_Null_Conditional_Operator()
{
if (_idsList?.Any() is false) { }
}
[Benchmark]
public void Array_Any_With_Null_Conditional_Operator()
{
if (_idsArray?.Any() is false) { }
}
[Benchmark]
public void Enumerable_Any_With_Null_Conditional_Operator()
{
if (_idsEnumerable?.Any() is false) { }
}
#endregion
#region Count_With_Pattern_Matching
[Benchmark]
public void List_Count_With_Pattern_Matching()
{
if (_idsList is { Count: > 0 } is false) { }
}
[Benchmark]
public void Array_Length_With_Pattern_Matching()
{
if (_idsArray is { Length: > 0 } is false) { }
}
[Benchmark]
public void Enumerable_Count_With_Pattern_Matching()
{
var list = _idsEnumerable?.ToList();
if (list is { Count: > 0 } is false) { }
}
#endregion
#region Count_With_Null_Conditional_Operator
[Benchmark]
public void List_Count_With_Null_Conditional_Operator()
{
if (_idsList?.Count == 0) { }
}
[Benchmark]
public void Array_Length_With_Null_Conditional_Operator()
{
if (_idsArray?.Length == 0) { }
}
[Benchmark]
public void Enumerable_Count_With_Null_Conditional_Operator()
{
if (_idsEnumerable?.Count() == 0) { }
}
#endregion
#region Count_With_Null_Equality_Check
[Benchmark]
public void List_Count_With_Null_Equality_Check()
{
if (_idsList == null || _idsList.Count == 0) { }
}
[Benchmark]
public void Array_Length_With_Null_Equality_Check()
{
if (_idsArray == null || _idsArray.Length == 0) { }
}
[Benchmark]
public void Enumerable_Count_With_Null_Equality_Check()
{
if (_idsEnumerable == null || _idsEnumerable.Count() == 0) { }
}
#endregion
}
}
public static bool TryGetNonEnumeratedCount(this IEnumerable<T> source, out int count);
if (movies.TryGetNonEnumeratedCount(out int count))
{
Console.WriteLine($"The count is {count}");
}
else
{
Console.WriteLine("Could not get a count of movies without enumerating the collection");
}