Skip to content

Instantly share code, notes, and snippets.

@Aetopia
Created September 1, 2024 17:34
Show Gist options
  • Save Aetopia/a06ae7a055b9118b90de23c7081b7863 to your computer and use it in GitHub Desktop.
Save Aetopia/a06ae7a055b9118b90de23c7081b7863 to your computer and use it in GitHub Desktop.
System.Box
using System;
using BenchmarkDotNet.Attributes;
[MemoryDiagnoser]
public class Benchmarks
{
object @object;
Box<int> box;
[GlobalSetup]
public void Setup()
{
@object = int.MaxValue;
box = new() { Value = int.MaxValue };
}
[Benchmark(Description = "Boxing performed by the runtime.")]
public object RuntimeBoxing() => int.MaxValue;
[Benchmark(Description = "Boxing performed by the Box<T> class.")]
public Box<int> ClassBoxing() => new() { Value = int.MaxValue };
[Benchmark(Description = "Unboxing performed by the runtime.")]
public int RuntimeUnboxing() => (int)@object;
[Benchmark(Description = "Unboxing performed by the Box<T> class.")]
public int ClassUnboxing() => box;
[Benchmark(Description = "Runtime boxing in a collection.")]
public object[] RuntimeBoxingCollection()
{
object[] objects = new object[100];
for (int index = 0; index < 100; index++) @objects[index] = index;
return objects;
}
[Benchmark(Description = "Box<T> class implicit boxing in a collection.")]
public object[] ClassBoxingCollection()
{
object[] objects = new object[100];
for (int index = 0; index < 100; index++) @objects[index] = new Box<int>() { Value = index };
return objects;
}
[Benchmark(Description = "Box<T> class explicit boxing in a collection.")]
public Box<int>[] ClassBoxingExplicitCollection()
{
Box<int>[] objects = new Box<int>[100];
for (int index = 0; index < 100; index++) @objects[index] = new Box<int>() { Value = index };
return objects;
}
}
.NET Framework
| Method | Mean | Error | StdDev | Gen0 | Gen1 | Allocated |
|------------------------------------------------ |------------:|----------:|----------:|-------:|-------:|----------:|
| 'Boxing performed by the runtime.' | 1.7105 ns | 0.0566 ns | 0.0501 ns | 0.0038 | - | 24 B |
| 'Boxing performed by the Box<T> class.' | 1.4196 ns | 0.0831 ns | 0.0889 ns | 0.0038 | - | 24 B |
| 'Unboxing performed by the runtime.' | 0.2887 ns | 0.0009 ns | 0.0008 ns | - | - | - |
| 'Unboxing performed by the Box<T> class.' | 0.0053 ns | 0.0002 ns | 0.0002 ns | - | - | - |
| 'Runtime boxing in a collection.' | 408.6101 ns | 1.3932 ns | 1.0877 ns | 0.5136 | 0.0057 | 3233 B |
| 'Box<T> class implicit boxing in a collection.' | 410.4538 ns | 4.1383 ns | 3.6685 ns | 0.5136 | 0.0057 | 3233 B |
| 'Box<T> class explicit boxing in a collection.' | 368.2353 ns | 3.4627 ns | 3.2390 ns | 0.5136 | 0.0057 | 3233 B |
.NET 8
| Method | Mean | Error | StdDev | Median | Gen0 | Gen1 | Allocated |
|------------------------------------------------ |------------:|----------:|----------:|------------:|-------:|-------:|----------:|
| 'Boxing performed by the runtime.' | 3.2346 ns | 0.1169 ns | 0.2734 ns | 3.0730 ns | 0.0029 | - | 24 B |
| 'Boxing performed by the Box<T> class.' | 2.8419 ns | 0.0092 ns | 0.0086 ns | 2.8373 ns | 0.0029 | - | 24 B |
| 'Unboxing performed by the runtime.' | 0.4266 ns | 0.0005 ns | 0.0004 ns | 0.4267 ns | - | - | - |
| 'Unboxing performed by the Box<T> class.' | 0.0000 ns | 0.0000 ns | 0.0000 ns | 0.0000 ns | - | - | - |
| 'Runtime boxing in a collection.' | 350.8327 ns | 1.3512 ns | 1.2640 ns | 351.1452 ns | 0.3853 | 0.0043 | 3224 B |
| 'Box<T> class implicit boxing in a collection.' | 348.1842 ns | 1.4172 ns | 1.1834 ns | 348.5104 ns | 0.3853 | 0.0043 | 3224 B |
| 'Box<T> class explicit boxing in a collection.' | 341.9604 ns | 1.8653 ns | 1.6536 ns | 342.5757 ns | 0.3853 | 0.0043 | 3224 B |
namespace System;
public sealed class Box<T> where T : struct
{
public T Value;
public static implicit operator T(Box<T> _) => _.Value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment