Last active
October 24, 2021 08:48
-
-
Save guitarrapc/b90f3057597350c0e3e9571f503702cc to your computer and use it in GitHub Desktop.
Nullable optimization. use is to determine Nullable<T> to T. https://twitter.com/badamczewski01/status/1452188929747308544
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
BenchmarkRunner.Run<BenchmarkNullable>(); | |
[BenchmarkDotNet.Attributes.MemoryDiagnoser] | |
public class BenchmarkNullable | |
{ | |
[Benchmark] | |
public void Slow() | |
{ | |
NullaleSlow(0); | |
} | |
[Benchmark] | |
public void Fast() | |
{ | |
NullaleFast(0); | |
} | |
int? NullaleSlow(int? x) | |
{ | |
for (int i = 0; i < 1000; i++) | |
{ | |
x++; | |
} | |
return x; | |
} | |
int? NullaleFast(int? x) | |
{ | |
if (x is int i32) | |
{ | |
for (int i = 0; i < 1000; i++) | |
{ | |
i32++; | |
} | |
x = i32; | |
} | |
return x; | |
} | |
} |
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
// Validating benchmarks: | |
Assembly LINQPadQuery, Version=1.0.0.139, 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 | Allocated | | |
|------- |---------:|--------:|--------:|----------:| | |
| Slow | 648.4 ns | 7.86 ns | 7.35 ns | - | | |
| Fast | 223.5 ns | 0.62 ns | 0.55 ns | - | | |
// * Legends * | |
Mean : Arithmetic mean of all measurements | |
Error : Half of 99.9% confidence interval | |
StdDev : Standard deviation of all measurements | |
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:44 (44.52 sec), executed benchmarks: 2 | |
Global total time: 00:00:44 (44.56 sec), executed benchmarks: 2 | |
// * Artifacts cleanup * |
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
// Validating benchmarks: | |
Assembly LINQPadQuery, Version=1.0.0.6, 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 | Allocated | | |
|------- |---------:|--------:|--------:|----------:| | |
| Slow | 637.5 ns | 0.88 ns | 0.73 ns | - | | |
| Fast | 221.0 ns | 0.53 ns | 0.42 ns | - | | |
// * Legends * | |
Mean : Arithmetic mean of all measurements | |
Error : Half of 99.9% confidence interval | |
StdDev : Standard deviation of all measurements | |
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:41 (41.7 sec), executed benchmarks: 2 | |
Global total time: 00:00:41 (41.73 sec), executed benchmarks: 2 | |
// * Artifacts cleanup * |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment