Created
July 22, 2020 15:15
-
-
Save smoogipoo/810c033962b8a95289dbdabd9d84801b to your computer and use it in GitHub Desktop.
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
| [MemoryDiagnoser] | |
| public class BenchmarkMethodDelegateAllocations | |
| { | |
| private readonly List<int> list = new List<int> { 1, 2, 3 }; | |
| private int field = 2; | |
| [Benchmark(Baseline = true)] | |
| public int SafeMethod() | |
| { | |
| int res = 0; | |
| for (int i = 0; i < 1000; i++) | |
| res += safeMethod(); | |
| return res; | |
| } | |
| [Benchmark] | |
| public int AllocatingMethod() | |
| { | |
| int res = 0; | |
| for (int i = 0; i < 1000; i++) | |
| res += allocatingMethod(); | |
| return res; | |
| } | |
| private int safeMethod() | |
| { | |
| return 0; | |
| int local = 2; | |
| return list.Count(_ => 0 > local); | |
| } | |
| private int allocatingMethod() | |
| { | |
| return 0; | |
| int local = 2; | |
| return list.Count(_ => field > local); | |
| } | |
| } |
Author
smoogipoo
commented
Jul 22, 2020
| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
|---|---|---|---|---|---|---|---|---|---|
| SafeMethod | 547.6 ns | 1.08 ns | 1.01 ns | 1.00 | 0.00 | - | - | - | - |
| AllocatingMethod | 8,790.7 ns | 209.87 ns | 618.80 ns | 16.92 | 0.67 | 0.9460 | - | - | 32000 B |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment