Skip to content

Instantly share code, notes, and snippets.

@adams85
Created August 1, 2021 12:19
Show Gist options
  • Save adams85/9ce7322df796fd7bdd8d663812f26065 to your computer and use it in GitHub Desktop.
Save adams85/9ce7322df796fd7bdd8d663812f26065 to your computer and use it in GitHub Desktop.
Benchmark for iterating over random-access collections
using System;
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
namespace MyBenchmarks
{
[SimpleJob(RuntimeMoniker.NetCoreApp31)]
[SimpleJob(RuntimeMoniker.Net50)]
[SimpleJob(RuntimeMoniker.Net48)]
[MemoryDiagnoser]
[RankColumn]
[HtmlExporter]
[MarkdownExporterAttribute.Default]
public class Benchmark
{
int[] _array;
IReadOnlyList<int> _arrayIntf;
List<int> _list;
IReadOnlyList<int> _listIntf;
[Params(10, 1000)]
public int N;
[GlobalSetup]
public void GlobalSetup()
{
var random = new Random();
_arrayIntf = _array = Enumerable.Range(0, N).Select(_ => random.Next()).ToArray();
_listIntf = _list = _array.ToList();
}
[Benchmark]
public int Array_ForEach()
{
int result = 0;
foreach (var item in _array)
result += item;
return result;
}
[Benchmark]
public int Array_For()
{
int result = 0;
for (int i = 0; i < _array.Length; i++)
result += _array[i];
return result;
}
[Benchmark]
public int Array_ForWithCountCached()
{
int result = 0;
for (int i = 0, n = _array.Length; i < n; i++)
result += _array[i];
return result;
}
[Benchmark]
public int ArrayThroughIntf_ForEach()
{
int result = 0;
foreach (var item in _arrayIntf)
result += item;
return result;
}
[Benchmark]
public int ArrayThroughIntf_For()
{
int result = 0;
for (int i = 0; i < _arrayIntf.Count; i++)
result += _arrayIntf[i];
return result;
}
[Benchmark]
public int ArrayThroughIntf_ForWithCountCached()
{
int result = 0;
for (int i = 0, n = _arrayIntf.Count; i < n; i++)
result += _arrayIntf[i];
return result;
}
[Benchmark]
public int List_ForEach()
{
int result = 0;
foreach (var item in _list)
result += item;
return result;
}
[Benchmark]
public int List_For()
{
int result = 0;
for (int i = 0; i < _list.Count; i++)
result += _list[i];
return result;
}
[Benchmark]
public int List_ForWithCountCached()
{
int result = 0;
for (int i = 0, n = _list.Count; i < n; i++)
result += _list[i];
return result;
}
[Benchmark]
public int ListThroughIntf_ForEach()
{
int result = 0;
foreach (var item in _listIntf)
result += item;
return result;
}
[Benchmark]
public int ListThroughIntf_For()
{
int result = 0;
for (int i = 0; i < _listIntf.Count; i++)
result += _listIntf[i];
return result;
}
[Benchmark]
public int ListThroughIntf_ForWithCountCached()
{
int result = 0;
for (int i = 0, n = _listIntf.Count; i < n; i++)
result += _listIntf[i];
return result;
}
}
public class Program
{
public static void Main(string[] args)
{
BenchmarkRunner.Run<Benchmark>();
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net48;netcoreapp31;net5</TargetFrameworks>
<LangVersion>9</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.0" />
</ItemGroup>
</Project>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>MyBenchmarks.Benchmark-20210801-130820</title>
<style type="text/css">
table { border-collapse: collapse; display: block; width: 100%; overflow: auto; }
td, th { padding: 6px 13px; border: 1px solid #ddd; text-align: right; }
tr { background-color: #fff; border-top: 1px solid #ccc; }
tr:nth-child(even) { background: #f8f8f8; }
</style>
</head>
<body>
<pre><code>
BenchmarkDotNet=v0.13.0, OS=Windows 10.0.19041.1110 (2004/May2020Update/20H1)
AMD Ryzen 7 2700X, 1 CPU, 16 logical and 8 physical cores
.NET SDK=5.0.302
[Host] : .NET 5.0.8 (5.0.821.31504), X64 RyuJIT
.NET 5.0 : .NET 5.0.8 (5.0.821.31504), X64 RyuJIT
.NET Core 3.1 : .NET Core 3.1.17 (CoreCLR 4.700.21.31506, CoreFX 4.700.21.31502), X64 RyuJIT
.NET Framework 4.8 : .NET Framework 4.8 (4.8.4390.0), X64 RyuJIT
</code></pre>
<pre><code></code></pre>
<table>
<thead><tr><th> Method</th><th> Job</th><th> Runtime</th><th>N</th><th> Mean</th><th>Error</th><th>StdDev</th><th>Rank</th><th>Gen 0</th><th>Gen 1</th><th>Gen 2</th><th>Allocated</th>
</tr>
</thead><tbody><tr><td>Array_ForEach</td><td>.NET 5.0</td><td>.NET 5.0</td><td>10</td><td>2.764 ns</td><td>0.0314 ns</td><td>0.0278 ns</td><td>1</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>Array_For</td><td>.NET 5.0</td><td>.NET 5.0</td><td>10</td><td>5.730 ns</td><td>0.0063 ns</td><td>0.0056 ns</td><td>6</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>Array_ForWithCountCached</td><td>.NET 5.0</td><td>.NET 5.0</td><td>10</td><td>4.437 ns</td><td>0.0057 ns</td><td>0.0054 ns</td><td>3</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>ArrayThroughIntf_ForEach</td><td>.NET 5.0</td><td>.NET 5.0</td><td>10</td><td>54.627 ns</td><td>0.1866 ns</td><td>0.1746 ns</td><td>22</td><td>0.0076</td><td>-</td><td>-</td><td>32 B</td>
</tr><tr><td>ArrayThroughIntf_For</td><td>.NET 5.0</td><td>.NET 5.0</td><td>10</td><td>46.133 ns</td><td>0.1378 ns</td><td>0.1222 ns</td><td>19</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>ArrayThroughIntf_ForWithCountCached</td><td>.NET 5.0</td><td>.NET 5.0</td><td>10</td><td>29.207 ns</td><td>0.2206 ns</td><td>0.2063 ns</td><td>14</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>List_ForEach</td><td>.NET 5.0</td><td>.NET 5.0</td><td>10</td><td>33.670 ns</td><td>0.3623 ns</td><td>0.3389 ns</td><td>17</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>List_For</td><td>.NET 5.0</td><td>.NET 5.0</td><td>10</td><td>10.573 ns</td><td>0.0380 ns</td><td>0.0337 ns</td><td>9</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>List_ForWithCountCached</td><td>.NET 5.0</td><td>.NET 5.0</td><td>10</td><td>5.803 ns</td><td>0.0390 ns</td><td>0.0346 ns</td><td>6</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>ListThroughIntf_ForEach</td><td>.NET 5.0</td><td>.NET 5.0</td><td>10</td><td>83.733 ns</td><td>0.0443 ns</td><td>0.0393 ns</td><td>24</td><td>0.0095</td><td>-</td><td>-</td><td>40 B</td>
</tr><tr><td>ListThroughIntf_For</td><td>.NET 5.0</td><td>.NET 5.0</td><td>10</td><td>41.603 ns</td><td>0.2622 ns</td><td>0.2452 ns</td><td>18</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>ListThroughIntf_ForWithCountCached</td><td>.NET 5.0</td><td>.NET 5.0</td><td>10</td><td>29.753 ns</td><td>0.4594 ns</td><td>0.4297 ns</td><td>15</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>Array_ForEach</td><td>.NET Core 3.1</td><td>.NET Core 3.1</td><td>10</td><td>2.951 ns</td><td>0.0298 ns</td><td>0.0233 ns</td><td>2</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>Array_For</td><td>.NET Core 3.1</td><td>.NET Core 3.1</td><td>10</td><td>5.769 ns</td><td>0.0300 ns</td><td>0.0280 ns</td><td>6</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>Array_ForWithCountCached</td><td>.NET Core 3.1</td><td>.NET Core 3.1</td><td>10</td><td>4.488 ns</td><td>0.0227 ns</td><td>0.0212 ns</td><td>3</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>ArrayThroughIntf_ForEach</td><td>.NET Core 3.1</td><td>.NET Core 3.1</td><td>10</td><td>54.934 ns</td><td>0.1963 ns</td><td>0.1740 ns</td><td>22</td><td>0.0076</td><td>-</td><td>-</td><td>32 B</td>
</tr><tr><td>ArrayThroughIntf_For</td><td>.NET Core 3.1</td><td>.NET Core 3.1</td><td>10</td><td>46.648 ns</td><td>0.1736 ns</td><td>0.1539 ns</td><td>19</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>ArrayThroughIntf_ForWithCountCached</td><td>.NET Core 3.1</td><td>.NET Core 3.1</td><td>10</td><td>31.315 ns</td><td>0.0639 ns</td><td>0.0567 ns</td><td>16</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>List_ForEach</td><td>.NET Core 3.1</td><td>.NET Core 3.1</td><td>10</td><td>23.470 ns</td><td>0.1766 ns</td><td>0.1474 ns</td><td>11</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>List_For</td><td>.NET Core 3.1</td><td>.NET Core 3.1</td><td>10</td><td>10.472 ns</td><td>0.0115 ns</td><td>0.0096 ns</td><td>9</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>List_ForWithCountCached</td><td>.NET Core 3.1</td><td>.NET Core 3.1</td><td>10</td><td>5.880 ns</td><td>0.0119 ns</td><td>0.0106 ns</td><td>7</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>ListThroughIntf_ForEach</td><td>.NET Core 3.1</td><td>.NET Core 3.1</td><td>10</td><td>84.043 ns</td><td>0.4561 ns</td><td>0.4043 ns</td><td>24</td><td>0.0095</td><td>-</td><td>-</td><td>40 B</td>
</tr><tr><td>ListThroughIntf_For</td><td>.NET Core 3.1</td><td>.NET Core 3.1</td><td>10</td><td>41.587 ns</td><td>0.2087 ns</td><td>0.1850 ns</td><td>18</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>ListThroughIntf_ForWithCountCached</td><td>.NET Core 3.1</td><td>.NET Core 3.1</td><td>10</td><td>26.199 ns</td><td>0.0320 ns</td><td>0.0299 ns</td><td>13</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>Array_ForEach</td><td>.NET Framework 4.8</td><td>.NET Framework 4.8</td><td>10</td><td>5.295 ns</td><td>0.0210 ns</td><td>0.0186 ns</td><td>5</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>Array_For</td><td>.NET Framework 4.8</td><td>.NET Framework 4.8</td><td>10</td><td>6.116 ns</td><td>0.1225 ns</td><td>0.1086 ns</td><td>8</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>Array_ForWithCountCached</td><td>.NET Framework 4.8</td><td>.NET Framework 4.8</td><td>10</td><td>5.197 ns</td><td>0.0860 ns</td><td>0.0671 ns</td><td>4</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>ArrayThroughIntf_ForEach</td><td>.NET Framework 4.8</td><td>.NET Framework 4.8</td><td>10</td><td>59.928 ns</td><td>0.1523 ns</td><td>0.1272 ns</td><td>23</td><td>0.0076</td><td>-</td><td>-</td><td>32 B</td>
</tr><tr><td>ArrayThroughIntf_For</td><td>.NET Framework 4.8</td><td>.NET Framework 4.8</td><td>10</td><td>50.317 ns</td><td>0.0809 ns</td><td>0.0756 ns</td><td>21</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>ArrayThroughIntf_ForWithCountCached</td><td>.NET Framework 4.8</td><td>.NET Framework 4.8</td><td>10</td><td>29.719 ns</td><td>0.0416 ns</td><td>0.0389 ns</td><td>15</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>List_ForEach</td><td>.NET Framework 4.8</td><td>.NET Framework 4.8</td><td>10</td><td>25.563 ns</td><td>0.2815 ns</td><td>0.2634 ns</td><td>12</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>List_For</td><td>.NET Framework 4.8</td><td>.NET Framework 4.8</td><td>10</td><td>12.076 ns</td><td>0.0811 ns</td><td>0.0677 ns</td><td>10</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>List_ForWithCountCached</td><td>.NET Framework 4.8</td><td>.NET Framework 4.8</td><td>10</td><td>10.515 ns</td><td>0.0704 ns</td><td>0.0624 ns</td><td>9</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>ListThroughIntf_ForEach</td><td>.NET Framework 4.8</td><td>.NET Framework 4.8</td><td>10</td><td>85.769 ns</td><td>1.5745 ns</td><td>1.3148 ns</td><td>25</td><td>0.0095</td><td>-</td><td>-</td><td>40 B</td>
</tr><tr><td>ListThroughIntf_For</td><td>.NET Framework 4.8</td><td>.NET Framework 4.8</td><td>10</td><td>48.001 ns</td><td>0.1768 ns</td><td>0.1654 ns</td><td>20</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>ListThroughIntf_ForWithCountCached</td><td>.NET Framework 4.8</td><td>.NET Framework 4.8</td><td>10</td><td>25.306 ns</td><td>0.0888 ns</td><td>0.0830 ns</td><td>12</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>Array_ForEach</td><td>.NET 5.0</td><td>.NET 5.0</td><td>1000</td><td>333.620 ns</td><td>1.1839 ns</td><td>1.1075 ns</td><td>26</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>Array_For</td><td>.NET 5.0</td><td>.NET 5.0</td><td>1000</td><td>481.053 ns</td><td>3.4859 ns</td><td>3.2607 ns</td><td>28</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>Array_ForWithCountCached</td><td>.NET 5.0</td><td>.NET 5.0</td><td>1000</td><td>482.884 ns</td><td>3.5214 ns</td><td>3.2939 ns</td><td>28</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>ArrayThroughIntf_ForEach</td><td>.NET 5.0</td><td>.NET 5.0</td><td>1000</td><td>3,667.564 ns</td><td>10.9417 ns</td><td>10.2349 ns</td><td>37</td><td>0.0076</td><td>-</td><td>-</td><td>32 B</td>
</tr><tr><td>ArrayThroughIntf_For</td><td>.NET 5.0</td><td>.NET 5.0</td><td>1000</td><td>3,847.360 ns</td><td>1.3185 ns</td><td>1.0294 ns</td><td>38</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>ArrayThroughIntf_ForWithCountCached</td><td>.NET 5.0</td><td>.NET 5.0</td><td>1000</td><td>1,950.561 ns</td><td>18.2296 ns</td><td>17.0520 ns</td><td>33</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>List_ForEach</td><td>.NET 5.0</td><td>.NET 5.0</td><td>1000</td><td>1,902.189 ns</td><td>1.4401 ns</td><td>1.2766 ns</td><td>32</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>List_For</td><td>.NET 5.0</td><td>.NET 5.0</td><td>1000</td><td>982.177 ns</td><td>0.7568 ns</td><td>0.7079 ns</td><td>31</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>List_ForWithCountCached</td><td>.NET 5.0</td><td>.NET 5.0</td><td>1000</td><td>499.432 ns</td><td>3.8699 ns</td><td>3.4306 ns</td><td>29</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>ListThroughIntf_ForEach</td><td>.NET 5.0</td><td>.NET 5.0</td><td>1000</td><td>6,165.530 ns</td><td>100.0242 ns</td><td>93.5627 ns</td><td>40</td><td>0.0076</td><td>-</td><td>-</td><td>40 B</td>
</tr><tr><td>ListThroughIntf_For</td><td>.NET 5.0</td><td>.NET 5.0</td><td>1000</td><td>3,375.480 ns</td><td>6.3734 ns</td><td>5.6499 ns</td><td>36</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>ListThroughIntf_ForWithCountCached</td><td>.NET 5.0</td><td>.NET 5.0</td><td>1000</td><td>1,950.926 ns</td><td>16.7554 ns</td><td>15.6730 ns</td><td>33</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>Array_ForEach</td><td>.NET Core 3.1</td><td>.NET Core 3.1</td><td>1000</td><td>339.781 ns</td><td>1.0138 ns</td><td>0.8987 ns</td><td>27</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>Array_For</td><td>.NET Core 3.1</td><td>.NET Core 3.1</td><td>1000</td><td>481.480 ns</td><td>2.9302 ns</td><td>2.7409 ns</td><td>28</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>Array_ForWithCountCached</td><td>.NET Core 3.1</td><td>.NET Core 3.1</td><td>1000</td><td>477.041 ns</td><td>1.3758 ns</td><td>1.2869 ns</td><td>28</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>ArrayThroughIntf_ForEach</td><td>.NET Core 3.1</td><td>.NET Core 3.1</td><td>1000</td><td>3,651.063 ns</td><td>3.1690 ns</td><td>2.4742 ns</td><td>37</td><td>0.0076</td><td>-</td><td>-</td><td>32 B</td>
</tr><tr><td>ArrayThroughIntf_For</td><td>.NET Core 3.1</td><td>.NET Core 3.1</td><td>1000</td><td>3,848.144 ns</td><td>3.1317 ns</td><td>2.7761 ns</td><td>38</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>ArrayThroughIntf_ForWithCountCached</td><td>.NET Core 3.1</td><td>.NET Core 3.1</td><td>1000</td><td>2,426.128 ns</td><td>8.7288 ns</td><td>7.7379 ns</td><td>35</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>List_ForEach</td><td>.NET Core 3.1</td><td>.NET Core 3.1</td><td>1000</td><td>1,944.994 ns</td><td>1.1358 ns</td><td>1.0624 ns</td><td>33</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>List_For</td><td>.NET Core 3.1</td><td>.NET Core 3.1</td><td>1000</td><td>990.250 ns</td><td>3.6897 ns</td><td>3.0811 ns</td><td>31</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>List_ForWithCountCached</td><td>.NET Core 3.1</td><td>.NET Core 3.1</td><td>1000</td><td>491.413 ns</td><td>1.1696 ns</td><td>0.9767 ns</td><td>29</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>ListThroughIntf_ForEach</td><td>.NET Core 3.1</td><td>.NET Core 3.1</td><td>1000</td><td>6,304.621 ns</td><td>11.4006 ns</td><td>9.5200 ns</td><td>41</td><td>0.0076</td><td>-</td><td>-</td><td>40 B</td>
</tr><tr><td>ListThroughIntf_For</td><td>.NET Core 3.1</td><td>.NET Core 3.1</td><td>1000</td><td>3,396.596 ns</td><td>9.0306 ns</td><td>8.0053 ns</td><td>36</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>ListThroughIntf_ForWithCountCached</td><td>.NET Core 3.1</td><td>.NET Core 3.1</td><td>1000</td><td>1,934.384 ns</td><td>3.1213 ns</td><td>2.9197 ns</td><td>33</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>Array_ForEach</td><td>.NET Framework 4.8</td><td>.NET Framework 4.8</td><td>1000</td><td>494.689 ns</td><td>0.4791 ns</td><td>0.4001 ns</td><td>29</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>Array_For</td><td>.NET Framework 4.8</td><td>.NET Framework 4.8</td><td>1000</td><td>494.352 ns</td><td>0.6262 ns</td><td>0.5858 ns</td><td>29</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>Array_ForWithCountCached</td><td>.NET Framework 4.8</td><td>.NET Framework 4.8</td><td>1000</td><td>495.340 ns</td><td>0.3964 ns</td><td>0.3708 ns</td><td>29</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>ArrayThroughIntf_ForEach</td><td>.NET Framework 4.8</td><td>.NET Framework 4.8</td><td>1000</td><td>3,907.519 ns</td><td>6.7850 ns</td><td>6.3467 ns</td><td>38</td><td>0.0076</td><td>-</td><td>-</td><td>32 B</td>
</tr><tr><td>ArrayThroughIntf_For</td><td>.NET Framework 4.8</td><td>.NET Framework 4.8</td><td>1000</td><td>3,877.308 ns</td><td>5.0694 ns</td><td>4.2332 ns</td><td>38</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>ArrayThroughIntf_ForWithCountCached</td><td>.NET Framework 4.8</td><td>.NET Framework 4.8</td><td>1000</td><td>2,424.867 ns</td><td>3.9312 ns</td><td>3.4849 ns</td><td>35</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>List_ForEach</td><td>.NET Framework 4.8</td><td>.NET Framework 4.8</td><td>1000</td><td>1,938.880 ns</td><td>2.3682 ns</td><td>2.2152 ns</td><td>33</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>List_For</td><td>.NET Framework 4.8</td><td>.NET Framework 4.8</td><td>1000</td><td>999.411 ns</td><td>9.0302 ns</td><td>8.4469 ns</td><td>31</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>List_ForWithCountCached</td><td>.NET Framework 4.8</td><td>.NET Framework 4.8</td><td>1000</td><td>736.952 ns</td><td>2.7980 ns</td><td>2.4803 ns</td><td>30</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>ListThroughIntf_ForEach</td><td>.NET Framework 4.8</td><td>.NET Framework 4.8</td><td>1000</td><td>5,585.675 ns</td><td>14.7446 ns</td><td>12.3124 ns</td><td>39</td><td>0.0076</td><td>-</td><td>-</td><td>40 B</td>
</tr><tr><td>ListThroughIntf_For</td><td>.NET Framework 4.8</td><td>.NET Framework 4.8</td><td>1000</td><td>3,631.933 ns</td><td>4.0610 ns</td><td>3.7987 ns</td><td>37</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr><tr><td>ListThroughIntf_ForWithCountCached</td><td>.NET Framework 4.8</td><td>.NET Framework 4.8</td><td>1000</td><td>2,279.616 ns</td><td>3.0973 ns</td><td>2.8972 ns</td><td>34</td><td>-</td><td>-</td><td>-</td><td>-</td>
</tr></tbody></table>
</body>
</html>
@mjebrahimi
Copy link

Results in markdown format

BenchmarkDotNet=v0.13.0, OS=Windows 10.0.19041.1110 (2004/May2020Update/20H1)
AMD Ryzen 7 2700X, 1 CPU, 16 logical and 8 physical cores
.NET SDK=5.0.302
  [Host]             : .NET 5.0.8 (5.0.821.31504), X64 RyuJIT
  .NET 5.0           : .NET 5.0.8 (5.0.821.31504), X64 RyuJIT
  .NET Core 3.1      : .NET Core 3.1.17 (CoreCLR 4.700.21.31506, CoreFX 4.700.21.31502), X64 RyuJIT
  .NET Framework 4.8 : .NET Framework 4.8 (4.8.4390.0), X64 RyuJIT
Method Job Runtime N Mean Error StdDev Rank Gen 0 Gen 1 Gen 2 Allocated
Array_ForEach .NET 5.0 .NET 5.0 10 2.764 ns 0.0314 ns 0.0278 ns 1 - - - -
Array_For .NET 5.0 .NET 5.0 10 5.730 ns 0.0063 ns 0.0056 ns 6 - - - -
Array_ForWithCountCached .NET 5.0 .NET 5.0 10 4.437 ns 0.0057 ns 0.0054 ns 3 - - - -
ArrayThroughIntf_ForEach .NET 5.0 .NET 5.0 10 54.627 ns 0.1866 ns 0.1746 ns 22 0.0076 - - 32 B
ArrayThroughIntf_For .NET 5.0 .NET 5.0 10 46.133 ns 0.1378 ns 0.1222 ns 19 - - - -
ArrayThroughIntf_ForWithCountCached .NET 5.0 .NET 5.0 10 29.207 ns 0.2206 ns 0.2063 ns 14 - - - -
List_ForEach .NET 5.0 .NET 5.0 10 33.670 ns 0.3623 ns 0.3389 ns 17 - - - -
List_For .NET 5.0 .NET 5.0 10 10.573 ns 0.0380 ns 0.0337 ns 9 - - - -
List_ForWithCountCached .NET 5.0 .NET 5.0 10 5.803 ns 0.0390 ns 0.0346 ns 6 - - - -
ListThroughIntf_ForEach .NET 5.0 .NET 5.0 10 83.733 ns 0.0443 ns 0.0393 ns 24 0.0095 - - 40 B
ListThroughIntf_For .NET 5.0 .NET 5.0 10 41.603 ns 0.2622 ns 0.2452 ns 18 - - - -
ListThroughIntf_ForWithCountCached .NET 5.0 .NET 5.0 10 29.753 ns 0.4594 ns 0.4297 ns 15 - - - -
Array_ForEach .NET Core 3.1 .NET Core 3.1 10 2.951 ns 0.0298 ns 0.0233 ns 2 - - - -
Array_For .NET Core 3.1 .NET Core 3.1 10 5.769 ns 0.0300 ns 0.0280 ns 6 - - - -
Array_ForWithCountCached .NET Core 3.1 .NET Core 3.1 10 4.488 ns 0.0227 ns 0.0212 ns 3 - - - -
ArrayThroughIntf_ForEach .NET Core 3.1 .NET Core 3.1 10 54.934 ns 0.1963 ns 0.1740 ns 22 0.0076 - - 32 B
ArrayThroughIntf_For .NET Core 3.1 .NET Core 3.1 10 46.648 ns 0.1736 ns 0.1539 ns 19 - - - -
ArrayThroughIntf_ForWithCountCached .NET Core 3.1 .NET Core 3.1 10 31.315 ns 0.0639 ns 0.0567 ns 16 - - - -
List_ForEach .NET Core 3.1 .NET Core 3.1 10 23.470 ns 0.1766 ns 0.1474 ns 11 - - - -
List_For .NET Core 3.1 .NET Core 3.1 10 10.472 ns 0.0115 ns 0.0096 ns 9 - - - -
List_ForWithCountCached .NET Core 3.1 .NET Core 3.1 10 5.880 ns 0.0119 ns 0.0106 ns 7 - - - -
ListThroughIntf_ForEach .NET Core 3.1 .NET Core 3.1 10 84.043 ns 0.4561 ns 0.4043 ns 24 0.0095 - - 40 B
ListThroughIntf_For .NET Core 3.1 .NET Core 3.1 10 41.587 ns 0.2087 ns 0.1850 ns 18 - - - -
ListThroughIntf_ForWithCountCached .NET Core 3.1 .NET Core 3.1 10 26.199 ns 0.0320 ns 0.0299 ns 13 - - - -
Array_ForEach .NET Framework 4.8 .NET Framework 4.8 10 5.295 ns 0.0210 ns 0.0186 ns 5 - - - -
Array_For .NET Framework 4.8 .NET Framework 4.8 10 6.116 ns 0.1225 ns 0.1086 ns 8 - - - -
Array_ForWithCountCached .NET Framework 4.8 .NET Framework 4.8 10 5.197 ns 0.0860 ns 0.0671 ns 4 - - - -
ArrayThroughIntf_ForEach .NET Framework 4.8 .NET Framework 4.8 10 59.928 ns 0.1523 ns 0.1272 ns 23 0.0076 - - 32 B
ArrayThroughIntf_For .NET Framework 4.8 .NET Framework 4.8 10 50.317 ns 0.0809 ns 0.0756 ns 21 - - - -
ArrayThroughIntf_ForWithCountCached .NET Framework 4.8 .NET Framework 4.8 10 29.719 ns 0.0416 ns 0.0389 ns 15 - - - -
List_ForEach .NET Framework 4.8 .NET Framework 4.8 10 25.563 ns 0.2815 ns 0.2634 ns 12 - - - -
List_For .NET Framework 4.8 .NET Framework 4.8 10 12.076 ns 0.0811 ns 0.0677 ns 10 - - - -
List_ForWithCountCached .NET Framework 4.8 .NET Framework 4.8 10 10.515 ns 0.0704 ns 0.0624 ns 9 - - - -
ListThroughIntf_ForEach .NET Framework 4.8 .NET Framework 4.8 10 85.769 ns 1.5745 ns 1.3148 ns 25 0.0095 - - 40 B
ListThroughIntf_For .NET Framework 4.8 .NET Framework 4.8 10 48.001 ns 0.1768 ns 0.1654 ns 20 - - - -
ListThroughIntf_ForWithCountCached .NET Framework 4.8 .NET Framework 4.8 10 25.306 ns 0.0888 ns 0.0830 ns 12 - - - -
Array_ForEach .NET 5.0 .NET 5.0 1000 333.620 ns 1.1839 ns 1.1075 ns 26 - - - -
Array_For .NET 5.0 .NET 5.0 1000 481.053 ns 3.4859 ns 3.2607 ns 28 - - - -
Array_ForWithCountCached .NET 5.0 .NET 5.0 1000 482.884 ns 3.5214 ns 3.2939 ns 28 - - - -
ArrayThroughIntf_ForEach .NET 5.0 .NET 5.0 1000 3,667.564 ns 10.9417 ns 10.2349 ns 37 0.0076 - - 32 B
ArrayThroughIntf_For .NET 5.0 .NET 5.0 1000 3,847.360 ns 1.3185 ns 1.0294 ns 38 - - - -
ArrayThroughIntf_ForWithCountCached .NET 5.0 .NET 5.0 1000 1,950.561 ns 18.2296 ns 17.0520 ns 33 - - - -
List_ForEach .NET 5.0 .NET 5.0 1000 1,902.189 ns 1.4401 ns 1.2766 ns 32 - - - -
List_For .NET 5.0 .NET 5.0 1000 982.177 ns 0.7568 ns 0.7079 ns 31 - - - -
List_ForWithCountCached .NET 5.0 .NET 5.0 1000 499.432 ns 3.8699 ns 3.4306 ns 29 - - - -
ListThroughIntf_ForEach .NET 5.0 .NET 5.0 1000 6,165.530 ns 100.0242 ns 93.5627 ns 40 0.0076 - - 40 B
ListThroughIntf_For .NET 5.0 .NET 5.0 1000 3,375.480 ns 6.3734 ns 5.6499 ns 36 - - - -
ListThroughIntf_ForWithCountCached .NET 5.0 .NET 5.0 1000 1,950.926 ns 16.7554 ns 15.6730 ns 33 - - - -
Array_ForEach .NET Core 3.1 .NET Core 3.1 1000 339.781 ns 1.0138 ns 0.8987 ns 27 - - - -
Array_For .NET Core 3.1 .NET Core 3.1 1000 481.480 ns 2.9302 ns 2.7409 ns 28 - - - -
Array_ForWithCountCached .NET Core 3.1 .NET Core 3.1 1000 477.041 ns 1.3758 ns 1.2869 ns 28 - - - -
ArrayThroughIntf_ForEach .NET Core 3.1 .NET Core 3.1 1000 3,651.063 ns 3.1690 ns 2.4742 ns 37 0.0076 - - 32 B
ArrayThroughIntf_For .NET Core 3.1 .NET Core 3.1 1000 3,848.144 ns 3.1317 ns 2.7761 ns 38 - - - -
ArrayThroughIntf_ForWithCountCached .NET Core 3.1 .NET Core 3.1 1000 2,426.128 ns 8.7288 ns 7.7379 ns 35 - - - -
List_ForEach .NET Core 3.1 .NET Core 3.1 1000 1,944.994 ns 1.1358 ns 1.0624 ns 33 - - - -
List_For .NET Core 3.1 .NET Core 3.1 1000 990.250 ns 3.6897 ns 3.0811 ns 31 - - - -
List_ForWithCountCached .NET Core 3.1 .NET Core 3.1 1000 491.413 ns 1.1696 ns 0.9767 ns 29 - - - -
ListThroughIntf_ForEach .NET Core 3.1 .NET Core 3.1 1000 6,304.621 ns 11.4006 ns 9.5200 ns 41 0.0076 - - 40 B
ListThroughIntf_For .NET Core 3.1 .NET Core 3.1 1000 3,396.596 ns 9.0306 ns 8.0053 ns 36 - - - -
ListThroughIntf_ForWithCountCached .NET Core 3.1 .NET Core 3.1 1000 1,934.384 ns 3.1213 ns 2.9197 ns 33 - - - -
Array_ForEach .NET Framework 4.8 .NET Framework 4.8 1000 494.689 ns 0.4791 ns 0.4001 ns 29 - - - -
Array_For .NET Framework 4.8 .NET Framework 4.8 1000 494.352 ns 0.6262 ns 0.5858 ns 29 - - - -
Array_ForWithCountCached .NET Framework 4.8 .NET Framework 4.8 1000 495.340 ns 0.3964 ns 0.3708 ns 29 - - - -
ArrayThroughIntf_ForEach .NET Framework 4.8 .NET Framework 4.8 1000 3,907.519 ns 6.7850 ns 6.3467 ns 38 0.0076 - - 32 B
ArrayThroughIntf_For .NET Framework 4.8 .NET Framework 4.8 1000 3,877.308 ns 5.0694 ns 4.2332 ns 38 - - - -
ArrayThroughIntf_ForWithCountCached .NET Framework 4.8 .NET Framework 4.8 1000 2,424.867 ns 3.9312 ns 3.4849 ns 35 - - - -
List_ForEach .NET Framework 4.8 .NET Framework 4.8 1000 1,938.880 ns 2.3682 ns 2.2152 ns 33 - - - -
List_For .NET Framework 4.8 .NET Framework 4.8 1000 999.411 ns 9.0302 ns 8.4469 ns 31 - - - -
List_ForWithCountCached .NET Framework 4.8 .NET Framework 4.8 1000 736.952 ns 2.7980 ns 2.4803 ns 30 - - - -
ListThroughIntf_ForEach .NET Framework 4.8 .NET Framework 4.8 1000 5,585.675 ns 14.7446 ns 12.3124 ns 39 0.0076 - - 40 B
ListThroughIntf_For .NET Framework 4.8 .NET Framework 4.8 1000 3,631.933 ns 4.0610 ns 3.7987 ns 37 - - - -
ListThroughIntf_ForWithCountCached .NET Framework 4.8 .NET Framework 4.8 1000 2,279.616 ns 3.0973 ns 2.8972 ns 34 - - - -

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment