回答編集履歴
3
修正
test
CHANGED
@@ -198,7 +198,7 @@
|
|
198
198
|
|
199
199
|
[Benchmark]
|
200
200
|
|
201
|
-
public string UseRef
|
201
|
+
public string UseReflection()
|
202
202
|
|
203
203
|
{
|
204
204
|
|
@@ -252,7 +252,7 @@
|
|
252
252
|
|
253
253
|
|-------------- |------------:|-----------:|----------:|------------:|----------:|
|
254
254
|
|
255
|
-
| UseRef
|
255
|
+
| UseReflection | 202.8417 ns | 13.9314 ns | 0.7636 ns | 203.0229 ns | 48 B |
|
256
256
|
|
257
257
|
| UseArray | 0.0055 ns | 0.1723 ns | 0.0094 ns | 0.0000 ns | - |
|
258
258
|
|
2
修正
test
CHANGED
@@ -234,8 +234,6 @@
|
|
234
234
|
|
235
235
|
{
|
236
236
|
|
237
|
-
|
238
|
-
|
239
237
|
static void Main()
|
240
238
|
|
241
239
|
{
|
1
追記
test
CHANGED
@@ -65,3 +65,199 @@
|
|
65
65
|
}
|
66
66
|
|
67
67
|
```
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
文字列で値を持ってくるならDictionaryがありますし、別に文字列である必要が無いなら、配列やListもあります。
|
72
|
+
|
73
|
+
```cs
|
74
|
+
|
75
|
+
using System;
|
76
|
+
|
77
|
+
using System.Collections.Generic;
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
class Program
|
82
|
+
|
83
|
+
{
|
84
|
+
|
85
|
+
private static readonly string[] _ArrayMessage =
|
86
|
+
|
87
|
+
{
|
88
|
+
|
89
|
+
"ok",
|
90
|
+
|
91
|
+
"ng",
|
92
|
+
|
93
|
+
};
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
private static readonly Dictionary<string, string> _DictMessage = new Dictionary<string, string>()
|
98
|
+
|
99
|
+
{
|
100
|
+
|
101
|
+
{"Message1", "ok"},
|
102
|
+
|
103
|
+
{"Message2", "ng"},
|
104
|
+
|
105
|
+
};
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
static void Main()
|
110
|
+
|
111
|
+
{
|
112
|
+
|
113
|
+
Console.WriteLine(_ArrayMessage[0]);
|
114
|
+
|
115
|
+
Console.WriteLine(_DictMessage["Message1"]);
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
Console.ReadKey();
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
```
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
リフレクションの速度が非常に遅いと言われても、どれくらい遅いのかピンと来ないと思うので、[BenchmarkDotNet](https://github.com/dotnet/BenchmarkDotNet) の計測結果を載せておきます。
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
```cs
|
134
|
+
|
135
|
+
using BenchmarkDotNet.Attributes;
|
136
|
+
|
137
|
+
using BenchmarkDotNet.Diagnosers;
|
138
|
+
|
139
|
+
using BenchmarkDotNet.Running;
|
140
|
+
|
141
|
+
using System.Collections.Generic;
|
142
|
+
|
143
|
+
using System.Reflection;
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
public static class Message
|
148
|
+
|
149
|
+
{
|
150
|
+
|
151
|
+
public static string Message1 { get; } = "ok";
|
152
|
+
|
153
|
+
public static string Message2 { get; } = "ng";
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
//計測用テストクラス
|
160
|
+
|
161
|
+
[MemoryDiagnoser(false)]
|
162
|
+
|
163
|
+
[ShortRunJob]
|
164
|
+
|
165
|
+
public class Test
|
166
|
+
|
167
|
+
{
|
168
|
+
|
169
|
+
private static readonly string[] _ArrayMessage =
|
170
|
+
|
171
|
+
{
|
172
|
+
|
173
|
+
"ok",
|
174
|
+
|
175
|
+
"ng",
|
176
|
+
|
177
|
+
};
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
private static readonly Dictionary<string, string> _DictMessage = new Dictionary<string, string>()
|
182
|
+
|
183
|
+
{
|
184
|
+
|
185
|
+
{"Message1", "ok"},
|
186
|
+
|
187
|
+
{"Message2", "ng"},
|
188
|
+
|
189
|
+
};
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
private int _Index = 0;
|
194
|
+
|
195
|
+
private string _StringKey = "1";
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
[Benchmark]
|
200
|
+
|
201
|
+
public string UseRefrection()
|
202
|
+
|
203
|
+
{
|
204
|
+
|
205
|
+
var prop = typeof(Message).GetProperty(
|
206
|
+
|
207
|
+
"Message" + _StringKey,
|
208
|
+
|
209
|
+
BindingFlags.GetProperty | BindingFlags.Static | BindingFlags.Public);
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
return (string)prop.GetValue(null);
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
[Benchmark]
|
220
|
+
|
221
|
+
public string UseArray() => _ArrayMessage[_Index];
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
[Benchmark]
|
226
|
+
|
227
|
+
public string UseDictionary() => _DictMessage["Message" + _StringKey];
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
class Program
|
234
|
+
|
235
|
+
{
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
static void Main()
|
240
|
+
|
241
|
+
{
|
242
|
+
|
243
|
+
BenchmarkRunner.Run<Test>();
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
(計測結果)
|
252
|
+
|
253
|
+
| Method | Mean | Error | StdDev | Median | Allocated |
|
254
|
+
|
255
|
+
|-------------- |------------:|-----------:|----------:|------------:|----------:|
|
256
|
+
|
257
|
+
| UseRefrection | 202.8417 ns | 13.9314 ns | 0.7636 ns | 203.0229 ns | 48 B |
|
258
|
+
|
259
|
+
| UseArray | 0.0055 ns | 0.1723 ns | 0.0094 ns | 0.0000 ns | - |
|
260
|
+
|
261
|
+
| UseDictionary | 47.3129 ns | 9.5924 ns | 0.5258 ns | 47.3259 ns | 48 B |
|
262
|
+
|
263
|
+
```
|