Last active
January 30, 2026 17:41
-
-
Save udlose/d19e3abbf421522e5276f915c3ac9492 to your computer and use it in GitHub Desktop.
.NET Benchmark and Results for comparing iteration using foreach over ArrayList vs List<T> containing Value Type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [MemoryDiagnoser] | |
| [GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)] | |
| [CategoriesColumn] | |
| [Config(typeof(IterationBenchmarks.IterationBenchmarksConfig))] | |
| public class IterationBenchmarks | |
| { | |
| [Params(1_000, 10_000, 50_000)] | |
| public int Count; | |
| private ArrayList _arrayList = null; | |
| private List<int> _listOfInt = null; | |
| private Consumer _consumer = null; | |
| [GlobalSetup] | |
| public void GlobalSetup() | |
| { | |
| _arrayList = new ArrayList(Count); | |
| _listOfInt = new List<int>(Count); | |
| Random random = new Random(42); | |
| for (int i = 0; i < Count; i++) | |
| { | |
| int value = random.Next(); | |
| _arrayList.Add(value); // boxed | |
| _listOfInt.Add(value); | |
| } | |
| // Create once and reuse, so you don't benchmark Consumer construction | |
| _consumer = new Consumer(); | |
| } | |
| [Benchmark(Baseline = true, Description = "ArrayList<int> foreach")] | |
| [MethodImpl(MethodImplOptions.NoInlining)] | |
| public void ArrayList_Foreach() | |
| { | |
| foreach (object item in _arrayList) | |
| { | |
| int value = (int)item; // unbox | |
| _consumer.Consume(value); | |
| } | |
| } | |
| [Benchmark(Description = "List<int> foreach")] | |
| [MethodImpl(MethodImplOptions.NoInlining)] | |
| public void List_Foreach() | |
| { | |
| foreach (int item in _listOfInt) | |
| { | |
| _consumer.Consume(item); | |
| } | |
| } | |
| private class IterationBenchmarksConfig : ManualConfig | |
| { | |
| public IterationBenchmarksConfig() | |
| { | |
| SummaryStyle = BenchmarkDotNet.Reports.SummaryStyle.Default.WithRatioStyle(RatioStyle.Trend); | |
| } | |
| } | |
| } | |
| public static class Program | |
| { | |
| public static void Main(string[] args) | |
| { | |
| BenchmarkRunner.Run<IterationBenchmarks>(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Run time: 00:03:07 (187.09 sec), executed benchmarks: 6 | |
| // * Summary * | |
| // | |
| // BenchmarkDotNet v0.15.8, Windows 11 (10.0.26200.7628/25H2/2025Update/HudsonValley2) | |
| // AMD Ryzen 9 7945HX with Radeon Graphics 2.50GHz, 1 CPU, 32 logical and 16 physical cores | |
| // | |
| // .NET SDK 10.0.102 | |
| // [Host] : .NET 10.0.2 (10.0.2, 10.0.225.61305), X86 RyuJIT x86-64-v4 | |
| // | |
| //| Method | Count | Mean | Error | StdDev | Ratio | RatioSD | Allocated | Alloc Ratio | | |
| //|------------------------- |------ |-------------:|------------:|------------:|-------------:|--------:|----------:|------------:| | |
| //| 'ArrayList<int> foreach' | 1000 | 4,143.4 ns | 88.30 ns | 257.58 ns | baseline | | 28 B | | | |
| //| 'List<int> foreach' | 1000 | 569.3 ns | 9.91 ns | 16.01 ns | 7.28x faster | 0.49x | - | NA | | |
| //| | | | | | | | | | | |
| //| 'ArrayList<int> foreach' | 10000 | 39,048.2 ns | 763.83 ns | 938.05 ns | baseline | | 28 B | | | |
| //| 'List<int> foreach' | 10000 | 5,656.2 ns | 107.70 ns | 105.77 ns | 6.91x faster | 0.21x | - | NA | | |
| //| | | | | | | | | | | |
| //| 'ArrayList<int> foreach' | 50000 | 198,148.2 ns | 3,780.95 ns | 4,354.15 ns | baseline | | 29 B | | | |
| //| 'List<int> foreach' | 50000 | 27,824.2 ns | 278.48 ns | 217.42 ns | 7.12x faster | 0.16x | - | NA | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Run time: 00:06:45 (405.59 sec), executed benchmarks: 6 | |
| // * Summary * | |
| // | |
| // BenchmarkDotNet v0.15.8, Windows 11 (10.0.26200.7628/25H2/2025Update/HudsonValley2) | |
| // AMD Ryzen 9 7945HX with Radeon Graphics 2.50GHz, 1 CPU, 32 logical and 16 physical cores | |
| // | |
| // [Host] : .NET Framework 4.8.1 (4.8.9221.0), X86 LegacyJIT | |
| // DefaultJob : .NET Framework 4.8.1 (4.8.9221.0), X86 LegacyJIT | |
| // | |
| //| Method | Count | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Allocated | Alloc Ratio | | |
| //|------------------------- |------ |-----------:|----------:|-----------:|-------------:|--------:|-------:|----------:|------------:| | |
| //| 'ArrayList<int> foreach' | 1000 | 5.684 μs | 0.1128 μs | 0.2429 μs | baseline | | 0.0153 | 28 B | | | |
| //| 'List<int> foreach' | 1000 | 1.694 μs | 0.0321 μs | 0.0812 μs | 3.36x faster | 0.21x | - | - | NA | | |
| //| | | | | | | | | | | | |
| //| 'ArrayList<int> foreach' | 10000 | 58.354 μs | 1.1588 μs | 2.5919 μs | baseline | | - | 28 B | | | |
| //| 'List<int> foreach' | 10000 | 16.478 μs | 0.3501 μs | 1.0100 μs | 3.55x faster | 0.27x | - | - | NA | | |
| //| | | | | | | | | | | | |
| //| 'ArrayList<int> foreach' | 50000 | 276.818 μs | 5.4755 μs | 14.5203 μs | baseline | | - | 28 B | | | |
| //| 'List<int> foreach' | 50000 | 80.084 μs | 1.5888 μs | 4.4553 μs | 3.47x faster | 0.26x | - | - | NA | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment