Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active October 26, 2021 13:35
Show Gist options
  • Save guitarrapc/d0b2b686567ff8e4a40bc324f31df2e3 to your computer and use it in GitHub Desktop.
Save guitarrapc/d0b2b686567ff8e4a40bc324f31df2e3 to your computer and use it in GitHub Desktop.
Unbox T with Unsafe.Unbox<T>(val) will remove allocation. https://twitter.com/badamczewski01/status/1452918677855145988
BenchmarkRunner.Run<BenchmarkUnbox>();
[BenchmarkDotNet.Attributes.MemoryDiagnoser]
public class BenchmarkUnbox
{
[Benchmark]
public void Slow()
{
A(0);
}
[Benchmark]
public void Fast()
{
B(0);
}
void A(int x)
{
object o = x;
for (var i = 0; i < 1000; i++)
Inc((int)o);
}
object Inc(int x) => ++x;
void B(int x)
{
object o = x;
for (var i = 0; i < 1000; i++)
Unsafe.Unbox<int>(o)++;
}
}
// Validating benchmarks:
Assembly LINQPadQuery, Version=1.0.0.336, Culture=neutral, PublicKeyToken=null is located in temp. If you are running benchmarks from xUnit you need to disable shadow copy. It's not supported by design.
// ***** BenchmarkRunner: Start *****
// * Summary *
BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22000
AMD Ryzen 9 5900X, 1 CPU, 24 logical and 12 physical cores
.NET SDK=6.0.100-rc.2.21505.57
[Host] : .NET 5.0.11 (5.0.1121.47308), X64 RyuJIT
| Method | Mean | Error | StdDev | Gen 0 | Allocated |
|------- |-----------:|---------:|---------:|-------:|----------:|
| Slow | 3,261.2 ns | 14.15 ns | 13.23 ns | 1.4343 | 24,024 B |
| Fast | 219.2 ns | 0.36 ns | 0.32 ns | 0.0014 | 24 B |
// * Legends *
Mean : Arithmetic mean of all measurements
Error : Half of 99.9% confidence interval
StdDev : Standard deviation of all measurements
Gen 0 : GC Generation 0 collects per 1000 operations
Allocated : Allocated memory per single operation (managed only, inclusive, 1KB = 1024B)
1 ns : 1 Nanosecond (0.000000001 sec)
// * Diagnostic Output - MemoryDiagnoser *
// ***** BenchmarkRunner: End *****
// ** Remained 0 benchmark(s) to run **
Run time: 00:00:46 (46.71 sec), executed benchmarks: 2
Global total time: 00:00:46 (46.71 sec), executed benchmarks: 2
// * Artifacts cleanup *
// Validating benchmarks:
Assembly LINQPadQuery, Version=1.0.0.4, Culture=neutral, PublicKeyToken=null is located in temp. If you are running benchmarks from xUnit you need to disable shadow copy. It's not supported by design.
// ***** BenchmarkRunner: Start *****
// * Summary *
BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22000
AMD Ryzen 9 5900X, 1 CPU, 24 logical and 12 physical cores
.NET SDK=6.0.100-rc.2.21505.57
[Host] : .NET 6.0.0 (6.0.21.48005), X64 RyuJIT
| Method | Mean | Error | StdDev | Gen 0 | Allocated |
|------- |-----------:|---------:|---------:|-------:|----------:|
| Slow | 3,501.2 ns | 58.68 ns | 54.89 ns | 1.4343 | 24,024 B |
| Fast | 219.6 ns | 1.14 ns | 1.01 ns | 0.0014 | 24 B |
// * Legends *
Mean : Arithmetic mean of all measurements
Error : Half of 99.9% confidence interval
StdDev : Standard deviation of all measurements
Gen 0 : GC Generation 0 collects per 1000 operations
Allocated : Allocated memory per single operation (managed only, inclusive, 1KB = 1024B)
1 ns : 1 Nanosecond (0.000000001 sec)
// * Diagnostic Output - MemoryDiagnoser *
// ***** BenchmarkRunner: End *****
// ** Remained 0 benchmark(s) to run **
Run time: 00:00:48 (48.68 sec), executed benchmarks: 2
Global total time: 00:00:48 (48.72 sec), executed benchmarks: 2
// * Artifacts cleanup *
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment