64 lines
2.1 KiB
Plaintext
64 lines
2.1 KiB
Plaintext
![]() |
== Resources
|
||
|
|
||
|
=== Documentation
|
||
|
|
||
|
* https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.trueforall[List<T>.TrueForAll(Predicate<T>)]
|
||
|
* https://learn.microsoft.com/en-us/dotnet/api/system.array.trueforall[Array.TrueForAll<T>(T[\], Predicate<T>)]
|
||
|
* https://learn.microsoft.com/en-us/dotnet/api/system.collections.immutable.immutablelist-1.trueforall[ImmutableList<T>.TrueForAll(Predicate<T>)]
|
||
|
* https://learn.microsoft.com/en-us/dotnet/api/system.collections.immutable.immutablelist-1.builder.trueforall[ImmutableList<T>.Builder.TrueForAll(Predicate<T>)]
|
||
|
* https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.all[Enumerable.All<TSource>]
|
||
|
|
||
|
=== Benchmarks
|
||
|
|
||
|
[options="header"]
|
||
|
|===
|
||
|
| Method | Runtime | Mean | StdDev | Ratio | Allocated
|
||
|
| TrueForAll | .NET 7.0 | 1.302 ms | 0.0027 ms | 0.21 | 1 B
|
||
|
| All | .NET 7.0 | 6.279 ms | 0.0181 ms | 1.00 | 40004 B
|
||
|
| TrueForAll | .NET Framework 4.6.2 | 1.105 ms | 0.0142 ms | 0.22 | -
|
||
|
| All | .NET Framework 4.6.2 | 4.968 ms | 0.0143 ms | 1.00 | 40128 B
|
||
|
|===
|
||
|
|
||
|
The results were generated by running the following snippet with https://github.com/dotnet/BenchmarkDotNet[BenchmarkDotNet]:
|
||
|
|
||
|
[source,csharp]
|
||
|
----
|
||
|
private List<int> data;
|
||
|
|
||
|
[Params(10_000)]
|
||
|
public int N { get; set; }
|
||
|
|
||
|
[GlobalSetup]
|
||
|
public void Setup() =>
|
||
|
data = Enumerable.Range(0, N).Select(x => 42).ToList();
|
||
|
|
||
|
[Benchmark]
|
||
|
public void TrueForAll()
|
||
|
{
|
||
|
for (var i = 0; i < N; i++)
|
||
|
{
|
||
|
_ = data.TrueForAll(x => x == 42); // List<T>.TrueForAll
|
||
|
}
|
||
|
}
|
||
|
|
||
|
[Benchmark(Baseline = true)]
|
||
|
public void All()
|
||
|
{
|
||
|
for (var i = 0; i < N; i++)
|
||
|
{
|
||
|
_ = data.All(x => x == 42); // Enumerable.All<TSource>
|
||
|
}
|
||
|
}
|
||
|
----
|
||
|
|
||
|
Hardware configuration:
|
||
|
|
||
|
[source]
|
||
|
----
|
||
|
BenchmarkDotNet=v0.13.5, OS=Windows 10 (10.0.19045.2846/22H2/2022Update)
|
||
|
12th Gen Intel Core i7-12800H, 1 CPU, 20 logical and 14 physical cores
|
||
|
.NET SDK=7.0.203
|
||
|
[Host] : .NET 7.0.5 (7.0.523.17405), X64 RyuJIT AVX2
|
||
|
.NET 7.0 : .NET 7.0.5 (7.0.523.17405), X64 RyuJIT AVX2
|
||
|
.NET Framework 4.6.2 : .NET Framework 4.8 (4.8.4614.0), X64 RyuJIT VectorSize=256
|
||
|
----
|