From c3e4118e51d9e3b0f1e8c919e16ce3a048a5e70d Mon Sep 17 00:00:00 2001 From: Costin Zaharia <56015273+costin-zaharia-sonarsource@users.noreply.github.com> Date: Fri, 7 Jun 2024 10:55:49 +0200 Subject: [PATCH] Rule S6608: update benchmark sample code and report (#9412) --- analyzers/rspec/cs/S6608.html | 144 +++++++++++++++------------------- 1 file changed, 65 insertions(+), 79 deletions(-) diff --git a/analyzers/rspec/cs/S6608.html b/analyzers/rspec/cs/S6608.html index 284689832c5..f349909099f 100644 --- a/analyzers/rspec/cs/S6608.html +++ b/analyzers/rspec/cs/S6608.html @@ -69,75 +69,39 @@

Benchmarks

ElementAt

-

.NET 7.0

-

15,193.1 ns

-

233.47 ns

+

3,403.4 ns

+

28.52 ns

+

26.67 ns

Index

-

.NET 7.0

-

9,465.6 ns

-

148.16 ns

+

478.0 ns

+

6.93 ns

+

6.48 ns

First

-

.NET 7.0

-

7,790.2 ns

-

165.70 ns

+

6,160.0 ns

+

57.66 ns

+

53.93 ns

First_Index

-

.NET 7.0

-

398.5 ns

-

5.36 ns

+

485.7 ns

+

5.81 ns

+

5.15 ns

Last

-

.NET 7.0

-

7,398.2 ns

-

152.48 ns

+

6,034.3 ns

+

20.34 ns

+

16.98 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

+

408.3 ns

+

2.54 ns

+

2.38 ns

@@ -161,74 +125,96 @@

Glossary

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