1
- using System ;
2
- using BenchmarkDotNet . Attributes ;
3
- using BenchmarkDotNet . Configs ;
4
- using BenchmarkDotNet . Running ;
5
- using CreateInstanceFromType . Tests . TestClasses ;
6
-
7
- namespace CreateInstanceFromType . Benchmark
1
+ namespace CreateInstanceFromType . Benchmark
8
2
{
3
+ using System ;
4
+ using BenchmarkDotNet . Attributes ;
5
+ using BenchmarkDotNet . Columns ;
6
+ using BenchmarkDotNet . Configs ;
7
+ using BenchmarkDotNet . Diagnosers ;
8
+ using BenchmarkDotNet . Running ;
9
+ using Tests . TestClasses ;
10
+
9
11
public class Program
10
12
{
11
- [ MemoryDiagnoser ]
12
- [ GroupBenchmarksBy ( BenchmarkLogicalGroupRule . ByCategory ) ]
13
+ [ Config ( typeof ( Config ) ) ]
13
14
public class CreateInstanceFromTypeBenchmark
14
15
{
16
+ private class Config : ManualConfig
17
+ {
18
+ public Config ( )
19
+ {
20
+ AddDiagnoser ( MemoryDiagnoser . Default ) ;
21
+ AddLogicalGroupRules ( BenchmarkLogicalGroupRule . ByCategory ) ;
22
+ AddColumn ( new TagColumn ( "Params" , name => name . Substring ( name . LastIndexOf ( '_' ) + 1 ) ) ) ;
23
+ AddColumn ( new TagColumn ( "Args" , name => name [ 0 ] == 'R' ? "Runtime" : "Design-time" ) ) ;
24
+ }
25
+ }
26
+
15
27
private const string _parameterless = "Parameterless" ;
16
28
private const string _oneParamCtor = "One Param" ;
17
29
private const string _twoParamsCtor = "Two Params" ;
18
30
private const string _threeParamsCtor = "Three Params" ;
19
31
20
- [ Benchmark ( Baseline = true , Description = "New" ) , BenchmarkCategory ( _parameterless ) ]
21
- public object New_0 ( )
32
+ private const string _designTimeArgs = "Design-time args" ;
33
+ private const string _runtimeArgs = "Runtime args" ;
34
+
35
+ #region New (Design-time Args)
36
+
37
+ [ BenchmarkCategory ( _designTimeArgs , _parameterless ) ]
38
+ [ Benchmark ( Baseline = true , Description = "New" ) ]
39
+ public object D_New_0 ( )
22
40
{
23
41
return new Parameterless ( ) ;
24
42
}
25
43
26
- [ Benchmark ( Baseline = true , Description = "New" ) , BenchmarkCategory ( _oneParamCtor ) ]
27
- public object New_1 ( )
44
+ [ BenchmarkCategory ( _designTimeArgs , _oneParamCtor ) ]
45
+ [ Benchmark ( Baseline = true , Description = "New" ) ]
46
+ public object D_New_1 ( )
28
47
{
29
48
return new OneParamCtor ( "hello!" ) ;
30
49
}
31
50
32
- [ Benchmark ( Baseline = true , Description = "New" ) , BenchmarkCategory ( _twoParamsCtor ) ]
33
- public object New_2 ( )
51
+ [ BenchmarkCategory ( _designTimeArgs , _twoParamsCtor ) ]
52
+ [ Benchmark ( Baseline = true , Description = "New" ) ]
53
+ public object D_New_2 ( )
34
54
{
35
55
return new TwoParamCtor ( "hello!" , 123 ) ;
36
56
}
37
57
38
- [ Benchmark ( Baseline = true , Description = "New" ) , BenchmarkCategory ( _threeParamsCtor ) ]
39
- public object New_3 ( )
58
+ [ BenchmarkCategory ( _designTimeArgs , _threeParamsCtor ) ]
59
+ [ Benchmark ( Baseline = true , Description = "New" ) ]
60
+ public object D_New_3 ( )
40
61
{
41
62
return new MultiCtor ( "hello!" , 123 , DateTime . MinValue ) ;
42
63
}
43
64
44
- #region Activator.CreateInstance
45
-
46
- [ Benchmark ( Description = "Activator" ) , BenchmarkCategory ( _parameterless ) ]
47
- public object ActivatorCreateInstance_0 ( )
48
- {
49
- return Activator . CreateInstance ( typeof ( Parameterless ) ) ;
50
- }
51
-
52
- [ Benchmark ( Description = "Activator" ) , BenchmarkCategory ( _oneParamCtor ) ]
53
- public object ActivatorCreateInstance_1 ( )
54
- {
55
- return Activator . CreateInstance ( typeof ( OneParamCtor ) , "hello!" ) ;
56
- }
57
-
58
- [ Benchmark ( Description = "Activator" ) , BenchmarkCategory ( _twoParamsCtor ) ]
59
- public object ActivatorCreateInstance_2 ( )
60
- {
61
- return Activator . CreateInstance ( typeof ( TwoParamCtor ) , "hello!" , 123 ) ;
62
- }
63
-
64
- [ Benchmark ( Description = "Activator" ) , BenchmarkCategory ( _threeParamsCtor ) ]
65
- public object ActivatorCreateInstance_3 ( )
66
- {
67
- return Activator . CreateInstance ( typeof ( MultiCtor ) , "hello!" , 123 , DateTime . MinValue ) ;
68
- }
69
-
70
65
#endregion
71
66
72
- #region 2012
67
+ #region 2012 (Design-time Args)
73
68
74
- [ Benchmark ( Description = "2012" ) , BenchmarkCategory ( _parameterless ) ]
75
- public object CreateInstanceFromType_2012_0 ( )
69
+ [ BenchmarkCategory ( _designTimeArgs , _parameterless ) ]
70
+ [ Benchmark ( Description = "2012" ) ]
71
+ public object D_2012_CreateInstanceFromType_0 ( )
76
72
{
77
73
return CreateInstanceFromType2012
78
74
. GetInstance ( typeof ( Parameterless ) ) ;
79
75
}
80
76
81
- [ Benchmark ( Description = "2012" ) , BenchmarkCategory ( _oneParamCtor ) ]
82
- public object CreateInstanceFromType_2012_1 ( )
77
+ [ BenchmarkCategory ( _designTimeArgs , _oneParamCtor ) ]
78
+ [ Benchmark ( Description = "2012" ) ]
79
+ public object D_2012_CreateInstanceFromType_1 ( )
83
80
{
84
81
return CreateInstanceFromType2012
85
82
. GetInstance ( typeof ( OneParamCtor ) , "hello!" ) ;
86
83
}
87
84
88
- [ Benchmark ( Description = "2012" ) , BenchmarkCategory ( _twoParamsCtor ) ]
89
- public object CreateInstanceFromType_2012_2 ( )
85
+ [ BenchmarkCategory ( _designTimeArgs , _twoParamsCtor ) ]
86
+ [ Benchmark ( Description = "2012" ) ]
87
+ public object D_2012_CreateInstanceFromType_2 ( )
90
88
{
91
89
return CreateInstanceFromType2012
92
90
. GetInstance ( typeof ( TwoParamCtor ) , "hello!" , 123 ) ;
93
91
}
94
92
95
- [ Benchmark ( Description = "2012" ) , BenchmarkCategory ( _threeParamsCtor ) ]
96
- public object CreateInstanceFromType_2012_3 ( )
93
+ [ BenchmarkCategory ( _designTimeArgs , _threeParamsCtor ) ]
94
+ [ Benchmark ( Description = "2012" ) ]
95
+ public object D_2012_CreateInstanceFromType_3 ( )
97
96
{
98
97
return CreateInstanceFromType2012
99
98
. GetInstance ( typeof ( MultiCtor ) , "hello!" , 123 , DateTime . MinValue ) ;
@@ -103,98 +102,107 @@ public object CreateInstanceFromType_2012_3()
103
102
104
103
#region 2020 Design-Time Args
105
104
106
- [ Benchmark ( Description = "2020" ) , BenchmarkCategory ( _parameterless ) ]
107
- public object CreateInstanceFromType_2020_D0 ( )
105
+ [ BenchmarkCategory ( _designTimeArgs , _parameterless ) ]
106
+ [ Benchmark ( Description = "2020" ) ]
107
+ public object D_2020_CreateInstanceFromType_0 ( )
108
108
{
109
109
return CreateInstanceFromType2020DesignTimeArgs
110
110
. GetInstance ( typeof ( Parameterless ) ) ;
111
111
}
112
112
113
- [ Benchmark ( Description = "2020" ) , BenchmarkCategory ( _oneParamCtor ) ]
114
- public object CreateInstanceFromType_2020_D1 ( )
113
+ [ BenchmarkCategory ( _designTimeArgs , _oneParamCtor ) ]
114
+ [ Benchmark ( Description = "2020" ) ]
115
+ public object D_2020_CreateInstanceFromType_1 ( )
115
116
{
116
117
return CreateInstanceFromType2020DesignTimeArgs
117
118
. GetInstance ( typeof ( OneParamCtor ) , "hello!" ) ;
118
119
}
119
120
120
- [ Benchmark ( Description = "2020" ) , BenchmarkCategory ( _twoParamsCtor ) ]
121
- public object CreateInstanceFromType_2020_D2 ( )
121
+ [ BenchmarkCategory ( _designTimeArgs , _twoParamsCtor ) ]
122
+ [ Benchmark ( Description = "2020" ) ]
123
+ public object D_2020_CreateInstanceFromType_2 ( )
122
124
{
123
125
return CreateInstanceFromType2020DesignTimeArgs
124
126
. GetInstance ( typeof ( TwoParamCtor ) , "hello!" , 123 ) ;
125
127
}
126
128
127
- [ Benchmark ( Description = "2020" ) , BenchmarkCategory ( _threeParamsCtor ) ]
128
- public object CreateInstanceFromType_2020_D3 ( )
129
+ [ BenchmarkCategory ( _designTimeArgs , _threeParamsCtor ) ]
130
+ [ Benchmark ( Description = "2020" ) ]
131
+ public object D_2020_CreateInstanceFromType_3 ( )
129
132
{
130
133
return CreateInstanceFromType2020DesignTimeArgs
131
134
. GetInstance ( typeof ( MultiCtor ) , "hello!" , 123 , DateTime . MinValue ) ;
132
135
}
133
136
134
137
#endregion
135
138
136
- #region 2020 Runtime Args
139
+ #region Activator.CreateInstance ( Runtime Args)
137
140
138
- [ Benchmark ( Description = "2020" ) , BenchmarkCategory ( _parameterless ) ]
139
- public object CreateInstanceFromType_2020_R0 ( )
141
+ [ BenchmarkCategory ( _runtimeArgs , _parameterless ) ]
142
+ [ Benchmark ( Baseline = true , Description = "Activator" ) ]
143
+ public object R_ActivatorCreateInstance_0 ( )
140
144
{
141
- return CreateInstanceFromType2020RuntimeArgs
142
- . GetInstance ( typeof ( Parameterless ) ) ;
145
+ return Activator . CreateInstance ( typeof ( Parameterless ) ) ;
143
146
}
144
147
145
- [ Benchmark ( Description = "2020" ) , BenchmarkCategory ( _oneParamCtor ) ]
146
- public object CreateInstanceFromType_2020_R1 ( )
148
+ [ BenchmarkCategory ( _runtimeArgs , _oneParamCtor ) ]
149
+ [ Benchmark ( Baseline = true , Description = "Activator" ) ]
150
+ public object R_ActivatorCreateInstance_1 ( )
147
151
{
148
- return CreateInstanceFromType2020RuntimeArgs
149
- . GetInstance ( typeof ( OneParamCtor ) , "hello!" ) ;
152
+ return Activator . CreateInstance ( typeof ( OneParamCtor ) , "hello!" ) ;
150
153
}
151
154
152
- [ Benchmark ( Description = "2020" ) , BenchmarkCategory ( _twoParamsCtor ) ]
153
- public object CreateInstanceFromType_2020_R2 ( )
155
+ [ BenchmarkCategory ( _runtimeArgs , _twoParamsCtor ) ]
156
+ [ Benchmark ( Baseline = true , Description = "Activator" ) ]
157
+ public object R_ActivatorCreateInstance_2 ( )
154
158
{
155
- return CreateInstanceFromType2020RuntimeArgs
156
- . GetInstance ( typeof ( TwoParamCtor ) , "hello!" , 123 ) ;
159
+ return Activator . CreateInstance ( typeof ( TwoParamCtor ) , "hello!" , 123 ) ;
157
160
}
158
161
159
- [ Benchmark ( Description = "2020" ) , BenchmarkCategory ( _threeParamsCtor ) ]
160
- public object CreateInstanceFromType_2020_R3 ( )
162
+ [ BenchmarkCategory ( _runtimeArgs , _threeParamsCtor ) ]
163
+ [ Benchmark ( Baseline = true , Description = "Activator" ) ]
164
+ public object R_ActivatorCreateInstance_3 ( )
161
165
{
162
- return CreateInstanceFromType2020RuntimeArgs
163
- . GetInstance ( typeof ( MultiCtor ) , "hello!" , 123 , DateTime . MinValue ) ;
166
+ return Activator . CreateInstance ( typeof ( MultiCtor ) , "hello!" , 123 , DateTime . MinValue ) ;
164
167
}
165
168
166
169
#endregion
167
- }
168
170
169
- [ MemoryDiagnoser ]
170
- public class CreateInstanceFromTypeOneParamCtor
171
- {
172
- [ Benchmark ( Baseline = true ) ]
173
- public object New ( )
171
+ #region 2020 Runtime Args
172
+
173
+ [ BenchmarkCategory ( _runtimeArgs , _parameterless ) ]
174
+ [ Benchmark ( Description = "2020" ) ]
175
+ public object R_2020_CreateInstanceFromType_0 ( )
174
176
{
175
- return new OneParamCtor ( "Hello!" ) ;
177
+ return CreateInstanceFromType2020RuntimeArgs
178
+ . GetInstance ( typeof ( Parameterless ) ) ;
176
179
}
177
180
178
- [ Benchmark ]
179
- public object ActivatorCreateInstance ( )
181
+ [ BenchmarkCategory ( _runtimeArgs , _oneParamCtor ) ]
182
+ [ Benchmark ( Description = "2020" ) ]
183
+ public object R_2020_CreateInstanceFromType_1 ( )
180
184
{
181
- return Activator
182
- . CreateInstance ( typeof ( OneParamCtor ) , "Hello !" ) ;
185
+ return CreateInstanceFromType2020RuntimeArgs
186
+ . GetInstance ( typeof ( OneParamCtor ) , "hello !" ) ;
183
187
}
184
188
185
- [ Benchmark ]
186
- public object CreateInstanceFromType_2012 ( )
189
+ [ BenchmarkCategory ( _runtimeArgs , _twoParamsCtor ) ]
190
+ [ Benchmark ( Description = "2020" ) ]
191
+ public object R_2020_CreateInstanceFromType_2 ( )
187
192
{
188
- return CreateInstanceFromType2012
189
- . GetInstance ( typeof ( OneParamCtor ) , "Hello!" ) ;
193
+ return CreateInstanceFromType2020RuntimeArgs
194
+ . GetInstance ( typeof ( TwoParamCtor ) , "hello!" , 123 ) ;
190
195
}
191
196
192
- [ Benchmark ]
193
- public object CreateInstanceFromType_2020 ( )
197
+ [ BenchmarkCategory ( _runtimeArgs , _threeParamsCtor ) ]
198
+ [ Benchmark ( Description = "2020" ) ]
199
+ public object R_2020_CreateInstanceFromType_3 ( )
194
200
{
195
201
return CreateInstanceFromType2020RuntimeArgs
196
- . GetInstance ( typeof ( OneParamCtor ) , "Hello!" ) ;
202
+ . GetInstance ( typeof ( MultiCtor ) , "hello!" , 123 , DateTime . MinValue ) ;
197
203
}
204
+
205
+ #endregion
198
206
}
199
207
200
208
public static void Main ( string [ ] args )
0 commit comments