137 lines
2.6 KiB
Plaintext
137 lines
2.6 KiB
Plaintext
== Resources
|
|
|
|
=== Documentation
|
|
|
|
* https://learn.microsoft.com/en-us/dotnet/api/system.collections.ilist.item[IList.Item[Int32\]]
|
|
* https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.ilist-1.item[IList<T>.Item[Int32\]]
|
|
* https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.ireadonlylist-1.item[IReadonlyList<T>.Item[Int32\]]
|
|
|
|
=== Benchmarks
|
|
|
|
[options="header"]
|
|
|===
|
|
| Method | Runtime | Mean | Standard Deviation
|
|
| ElementAt | 3,403.4 ns | 28.52 ns | 26.67 ns
|
|
| Index | 478.0 ns | 6.93 ns | 6.48 ns
|
|
| First | 6,160.0 ns | 57.66 ns | 53.93 ns
|
|
| First_Index | 485.7 ns | 5.81 ns | 5.15 ns
|
|
| Last | 6,034.3 ns | 20.34 ns | 16.98 ns
|
|
| Last_Index | 408.3 ns | 2.54 ns | 2.38 ns
|
|
|===
|
|
|
|
==== Glossary
|
|
|
|
* https://en.wikipedia.org/wiki/Arithmetic_mean[Mean]
|
|
* https://en.wikipedia.org/wiki/Standard_deviation[Standard Deviation]
|
|
|
|
The results were generated by running the following snippet with https://github.com/dotnet/BenchmarkDotNet[BenchmarkDotNet]:
|
|
|
|
[source,csharp]
|
|
----
|
|
private List<byte> data;
|
|
private Random random;
|
|
|
|
[Params(1_000_000)]
|
|
public int SampleSize;
|
|
|
|
[Params(1_000)]
|
|
public int LoopSize;
|
|
|
|
[GlobalSetup]
|
|
public void Setup()
|
|
{
|
|
random = new Random(42);
|
|
|
|
var bytes = new byte[SampleSize];
|
|
random.NextBytes(bytes);
|
|
data = bytes.ToList();
|
|
}
|
|
|
|
[Benchmark]
|
|
public int ElementAt()
|
|
{
|
|
int result = default;
|
|
|
|
for (var i = 0; i < LoopSize; i++)
|
|
{
|
|
result = data.ElementAt(i);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
[Benchmark]
|
|
public int Index()
|
|
{
|
|
int result = default;
|
|
|
|
for (var i = 0; i < LoopSize; i++)
|
|
{
|
|
result = data[i];
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
[Benchmark]
|
|
public int First()
|
|
{
|
|
int result = default;
|
|
|
|
for (var i = 0; i < LoopSize; i++)
|
|
{
|
|
result = data.First();
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
[Benchmark]
|
|
public int First_Index()
|
|
{
|
|
int result = default;
|
|
|
|
for (var i = 0; i < LoopSize; i++)
|
|
{
|
|
result = data[0];
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
[Benchmark]
|
|
public int Last()
|
|
{
|
|
int result = default;
|
|
|
|
for (var i = 0; i < LoopSize; i++)
|
|
{
|
|
result = data.Last();
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
[Benchmark]
|
|
public int Last_Index()
|
|
{
|
|
int result = default;
|
|
|
|
for (var i = 0; i < LoopSize; i++)
|
|
{
|
|
result = data[data.Count - 1];
|
|
}
|
|
|
|
return result;
|
|
}
|
|
----
|
|
|
|
Hardware configuration:
|
|
----
|
|
BenchmarkDotNet=v0.13.5, OS=Windows 10 (10.0.19045.4412/22H2/2022Update)
|
|
11th Gen Intel Core i7-11850H 2.50GHz, 1 CPU, 16 logical and 8 physical cores
|
|
.NET SDK=8.0.301
|
|
[Host] : .NET 8.0.6 (8.0.624.26715), X64 RyuJIT AVX2
|
|
.NET 8.0 : .NET 8.0.6 (8.0.624.26715), X64 RyuJIT AVX2
|
|
----
|