-
Notifications
You must be signed in to change notification settings - Fork 6
/
Program.cs
69 lines (57 loc) · 1.77 KB
/
Program.cs
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
// Made by Benjamin Abt - https://github.com/BenjaminAbt
using System.Collections.Generic;
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
{
// Class
[Benchmark]
[ArgumentsSource(nameof(Class_Arguments))]
public MyClass? Class_Condition(MyClass? myClass, string title)
{
if (myClass is not null) { return myClass; }
return null;
}
[Benchmark]
[ArgumentsSource(nameof(Class_Arguments))]
public MyClass? Class_PatternMatching(MyClass? myClass, string title)
{
if (myClass is MyClass val) { return val; }
return null;
}
public IEnumerable<object[]> Class_Arguments()
{
yield return new object[] { new MyClass(), "not null" };
yield return new object[] { null, "null" };
}
// Struct
[Benchmark]
[ArgumentsSource(nameof(Struct_Arguments))]
public MyStruct? Struct_HasValue(MyStruct? myStruct, string title)
{
if (myStruct.HasValue) { return myStruct; }
return null;
}
[Benchmark]
[ArgumentsSource(nameof(Struct_Arguments))]
public MyStruct? Struct_PatterMatching(MyStruct? myStruct, string title)
{
if (myStruct is MyStruct val) { return val; }
return null;
}
public IEnumerable<object[]> Struct_Arguments()
{
yield return new object[] { new MyStruct(), "not null" };
yield return new object[] { null, "null" };
}
}
public class MyClass { }
public readonly struct MyStruct { }