Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save udlose/1b530009e8487c727dc3a2193c5f090a to your computer and use it in GitHub Desktop.

Select an option

Save udlose/1b530009e8487c727dc3a2193c5f090a to your computer and use it in GitHub Desktop.
.NET Benchmark and Results for comparing iteration using foreach vs for over ArrayList containing Value Type
using System;
using System.Collections;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Engines;
using BenchmarkDotNet.Running;
[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 Consumer _consumer = null;
[GlobalSetup]
public void GlobalSetup()
{
_arrayList = new ArrayList(Count);
Random random = new Random(42);
for (int i = 0; i < Count; i++)
{
int value = random.Next();
_arrayList.Add(value); // boxed
}
// 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 = "ArrayList<int> for")]
[MethodImpl(MethodImplOptions.NoInlining)]
public void ArrayList_For()
{
for (int i = 0; i < _arrayList.Count; i++)
{
int value = (int)_arrayList[i]; // indexer returns object -> unbox
_consumer.Consume(value);
}
}
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>();
}
}
// Run time: 00:04:14 (254.71 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.535 μs | 0.1128 μs | 0.3218 μs | baseline | | 28 B | |
//| 'ArrayList<int> for' | 1000 | 2.441 μs | 0.0480 μs | 0.0980 μs | 1.86x faster | 0.15x | - | NA |
//| | | | | | | | | |
//| 'ArrayList<int> foreach' | 10000 | 44.173 μs | 0.8891 μs | 2.5652 μs | baseline | | 28 B | |
//| 'ArrayList<int> for' | 10000 | 23.144 μs | 0.4258 μs | 0.3324 μs | 1.91x faster | 0.11x | - | NA |
//| | | | | | | | | |
//| 'ArrayList<int> foreach' | 50000 | 201.273 μs | 3.6190 μs | 3.2081 μs | baseline | | 29 B | |
//| 'ArrayList<int> for' | 50000 | 114.972 μs | 2.0725 μs | 1.8372 μs | 1.75x faster | 0.04x | - | NA |
// Run time: 00:05:36 (336.18 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.447 μs | 0.0838 μs | 0.0743 μs | baseline | | 0.0153 | 28 B | |
//| 'ArrayList<int> for' | 1000 | 2.670 μs | 0.1083 μs | 0.3036 μs | 2.06x faster | 0.22x | - | - | NA |
//| | | | | | | | | | |
//| 'ArrayList<int> foreach' | 10000 | 58.263 μs | 1.2330 μs | 3.4574 μs | baseline | | - | 28 B | |
//| 'ArrayList<int> for' | 10000 | 26.278 μs | 0.5566 μs | 1.6323 μs | 2.23x faster | 0.19x | - | - | NA |
//| | | | | | | | | | |
//| 'ArrayList<int> foreach' | 50000 | 283.246 μs | 6.2218 μs | 18.3451 μs | baseline | | - | 28 B | |
//| 'ArrayList<int> for' | 50000 | 129.694 μs | 2.5740 μs | 4.9591 μs | 2.19x faster | 0.16x | - | - | NA |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment