Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save udlose/53ff58f81f99540c2fe4ed5debc4763c to your computer and use it in GitHub Desktop.

Select an option

Save udlose/53ff58f81f99540c2fe4ed5debc4763c to your computer and use it in GitHub Desktop.
ArrayList_foreach_object_vs_specific_type_ReferenceType_Benchmarks
[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);
for (int i = 0; i < Count; i++)
{
IPerson p = new Person(i, "Dwight Schrute");
_arrayList.Add(p); // boxed
}
// Create once and reuse, so you don't benchmark Consumer construction
_consumer = new Consumer();
}
[Benchmark(Baseline = true, Description = "IPerson p in _arrayList unboxing")]
[MethodImpl(MethodImplOptions.NoInlining)]
public void ArrayList_InlineBoxing()
{
foreach (IPerson p in _arrayList) // unbox
{
_consumer.Consume(p);
}
}
[Benchmark(Description = "object item in _arrayList unboxing (no cast to Person)")]
[MethodImpl(MethodImplOptions.NoInlining)]
public void ArrayList_DeferredBoxingNoCast()
{
foreach(object item in _arrayList)
{
_consumer.Consume(item);
}
}
[Benchmark(Description = "object item in _arrayList unboxing (with cast to Person)")]
[MethodImpl(MethodImplOptions.NoInlining)]
public void ArrayList_DeferredBoxingWithCast()
{
foreach (object item in _arrayList)
{
IPerson value = (Person)item; // 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>();
}
}
public interface IPerson
{
int Compute();
}
public class Person : IPerson
{
public readonly int Id;
public readonly string Name;
public Person(int id, string name)
{
Id = id;
Name = name;
}
public int Compute()
{
// Cheap deterministic work; no allocations; good for net48 + net8.
Random rand = new Random();
int x = rand.Next() ^ (Id * 397);
x = (x << 5) | (int)((uint)x >> 27);
x = x * 31 + Id;
return x;
}
}
// Run time: 00:05:54 (354.4 sec), executed benchmarks: 9
// * 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 |
//|----------------------------------------------------------- |------ |-----------:|----------:|-----------:|-------------:|--------:|----------:|------------:|
//| 'IPerson p in _arrayList unboxing' | 1000 | 6.430 μs | 0.1186 μs | 0.2772 μs | baseline | | 28 B | |
//| 'object item in _arrayList unboxing (no cast to Person)' | 1000 | 4.980 μs | 0.0966 μs | 0.1354 μs | 1.29x faster | 0.06x | 28 B | 1.00x more |
//| 'object item in _arrayList unboxing (with cast to Person)' | 1000 | 5.276 μs | 0.1025 μs | 0.1402 μs | 1.22x faster | 0.06x | 28 B | 1.00x more |
//| | | | | | | | | |
//| 'IPerson p in _arrayList unboxing' | 10000 | 60.196 μs | 1.1470 μs | 1.4506 μs | baseline | | 28 B | |
//| 'object item in _arrayList unboxing (no cast to Person)' | 10000 | 50.923 μs | 1.0110 μs | 1.3146 μs | 1.18x faster | 0.04x | 28 B | 1.00x more |
//| 'object item in _arrayList unboxing (with cast to Person)' | 10000 | 55.630 μs | 1.1079 μs | 1.9693 μs | 1.08x faster | 0.05x | 28 B | 1.00x more |
//| | | | | | | | | |
//| 'IPerson p in _arrayList unboxing' | 50000 | 316.902 μs | 6.2525 μs | 13.4592 μs | baseline | | 30 B | |
//| 'object item in _arrayList unboxing (no cast to Person)' | 50000 | 246.246 μs | 4.8686 μs | 8.7791 μs | 1.29x faster | 0.07x | 30 B | 1.00x more |
//| 'object item in _arrayList unboxing (with cast to Person)' | 50000 | 279.276 μs | 5.5766 μs | 13.5741 μs | 1.14x faster | 0.07x | 30 B | 1.00x more |
// Run time: 00:08:14 (494.46 sec), executed benchmarks: 9
// * 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 |
//|----------------------------------------------------------- |------ |-----------:|----------:|-----------:|-------------:|--------:|-------:|----------:|------------:|
//| 'IPerson p in _arrayList unboxing' | 1000 | 7.506 μs | 0.1494 μs | 0.3215 μs | baseline | | 0.0153 | 28 B | |
//| 'object item in _arrayList unboxing (no cast to Person)' | 1000 | 6.322 μs | 0.1216 μs | 0.1624 μs | 1.19x faster | 0.06x | 0.0153 | 28 B | 1.00x more |
//| 'object item in _arrayList unboxing (with cast to Person)' | 1000 | 6.501 μs | 0.1233 μs | 0.2192 μs | 1.16x faster | 0.06x | 0.0153 | 28 B | 1.00x more |
//| | | | | | | | | | |
//| 'IPerson p in _arrayList unboxing' | 10000 | 71.081 μs | 1.4214 μs | 3.0900 μs | baseline | | - | 28 B | |
//| 'object item in _arrayList unboxing (no cast to Person)' | 10000 | 61.790 μs | 1.2124 μs | 2.7366 μs | 1.15x faster | 0.07x | - | 28 B | 1.00x more |
//| 'object item in _arrayList unboxing (with cast to Person)' | 10000 | 67.654 μs | 1.3468 μs | 3.7094 μs | 1.05x faster | 0.07x | - | 28 B | 1.00x more |
//| | | | | | | | | | |
//| 'IPerson p in _arrayList unboxing' | 50000 | 352.559 μs | 7.0394 μs | 11.7612 μs | baseline | | - | 28 B | |
//| 'object item in _arrayList unboxing (no cast to Person)' | 50000 | 310.968 μs | 6.2138 μs | 14.2773 μs | 1.14x faster | 0.06x | - | 28 B | 1.00x more |
//| 'object item in _arrayList unboxing (with cast to Person)' | 50000 | 326.223 μs | 6.4886 μs | 14.9086 μs | 1.08x faster | 0.06x | - | 28 B | 1.00x more |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment