-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathProgram.cs
More file actions
87 lines (74 loc) · 1.76 KB
/
Copy pathProgram.cs
File metadata and controls
87 lines (74 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Made by Benjamin Abt - https://github.com/BenjaminAbt
using System.Text;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
BenchmarkRunner.Run<Benchmark>();
[MemoryDiagnoser]
[SimpleJob(RuntimeMoniker.Net70)] // PGO enabled by default
[SimpleJob(RuntimeMoniker.Net80)]
[SimpleJob(RuntimeMoniker.Net90, baseline: true)]
[HideColumns(Column.Job)]
public class Benchmark
{
[Params(10, 100, 1000)]
public int Iterations { get; set; }
private readonly TestClass _testClass = new();
[Benchmark]
public void WithoutLocal()
{
for (int i = 0; i < Iterations; i++)
{
_testClass.AnyHotPathMethod();
}
}
[Benchmark]
public void WithLocal()
{
for (int i = 0; i < Iterations; i++)
{
_testClass.AnyHotPathMethodWithFunction();
}
}
}
public class TestClass
{
private readonly StringBuilder _sb = new();
private readonly bool _b1;
private readonly bool _b2;
public void AnyHotPathMethod()
{
if (_b1)
{
// Do the things that need to be done
_sb.Append("aaa");
}
if (_b2)
{
// Do the things that need to be done
_sb.Append("bbb");
}
}
public void AnyHotPathMethodWithFunction()
{
if (_b1)
{
Core();
void Core()
{
// Do the things that need to be done
_sb.Append("aaa");
}
}
if (_b2)
{
Core();
void Core()
{
// Do the things that need to be done
_sb.Append("bbb");
}
}
}
}