Skip to content

Commit

Permalink
S6608: Update benchmarks code sample and results (#3980)
Browse files Browse the repository at this point in the history
  • Loading branch information
costin-zaharia-sonarsource authored Jun 6, 2024
1 parent 0e09fb7 commit 75e4db7
Showing 1 changed file with 53 additions and 37 deletions.
90 changes: 53 additions & 37 deletions rules/S6608/resources-dotnet.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,12 @@
[options="header"]
|===
| Method | Runtime | Mean | Standard Deviation
| ElementAt | .NET 7.0 | 15,193.1 ns | 233.47 ns
| Index | .NET 7.0 | 9,465.6 ns | 148.16 ns
| First | .NET 7.0 | 7,790.2 ns | 165.70 ns
| First_Index | .NET 7.0 | 398.5 ns | 5.36 ns
| Last | .NET 7.0 | 7,398.2 ns | 152.48 ns
| Last_Index | .NET 7.0 | 347.3 ns | 5.47 ns
| ElementAt | .NET Framework 4.6.2 | 12,205.7 ns | 298.49 ns
| Index | .NET Framework 4.6.2 | 8,917.8 ns | 51.55 ns
| First | .NET Framework 4.6.2 | 5,109.1 ns | 100.13 ns
| First_Index | .NET Framework 4.6.2 | 566.0 ns | 6.56 ns
| Last | .NET Framework 4.6.2 | 5,052.7 ns | 76.02 ns
| Last_Index | .NET Framework 4.6.2 | 680.7 ns | 9.56 ns
| 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
Expand All @@ -47,74 +41,96 @@ public int LoopSize;
public void Setup()
{
random = new Random(42);
var bytes = new byte[SampleSize];
random.NextBytes(bytes);
data = bytes.ToList();
}
[Benchmark]
public void ElementAt()
public int ElementAt()
{
for (int i = 0; i < LoopSize; i++)
int result = default;
for (var i = 0; i < LoopSize; i++)
{
var index = random.Next(0, SampleSize);
_ = data.ElementAt(index);
result = data.ElementAt(i);
}
return result;
}
[Benchmark]
public void Index()
public int Index()
{
for (int i = 0; i < LoopSize; i++)
int result = default;
for (var i = 0; i < LoopSize; i++)
{
var index = random.Next(0, SampleSize);
_ = data[index];
result = data[i];
}
return result;
}
[Benchmark]
public void First()
public int First()
{
for (int i = 0; i < LoopSize; i++)
int result = default;
for (var i = 0; i < LoopSize; i++)
{
_ = data.First();
result = data.First();
}
return result;
}
[Benchmark]
public void First_Index()
public int First_Index()
{
for (int i = 0; i < LoopSize; i++)
int result = default;
for (var i = 0; i < LoopSize; i++)
{
_ = data[0];
result = data[0];
}
return result;
}
[Benchmark]
public void Last()
public int Last()
{
for (int i = 0; i < LoopSize; i++)
int result = default;
for (var i = 0; i < LoopSize; i++)
{
_ = data.Last();
result = data.Last();
}
return result;
}
[Benchmark]
public void Last_Index()
public int Last_Index()
{
for (int i = 0; i < LoopSize; i++)
int result = default;
for (var i = 0; i < LoopSize; i++)
{
_ = data[data.Count - 1];
result = data[data.Count - 1];
}
return result;
}
----

Hardware configuration:
----
BenchmarkDotNet=v0.13.5, OS=Windows 10 (10.0.19045.2846/22H2/2022Update)
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=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.1 (4.8.9139.0), X64 RyuJIT VectorSize=256
.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
----

0 comments on commit 75e4db7

Please sign in to comment.