teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

修正

2021/12/01 01:52

投稿

退会済みユーザー
answer CHANGED
@@ -98,7 +98,7 @@
98
98
  private string _StringKey = "1";
99
99
 
100
100
  [Benchmark]
101
- public string UseRefrection()
101
+ public string UseReflection()
102
102
  {
103
103
  var prop = typeof(Message).GetProperty(
104
104
  "Message" + _StringKey,
@@ -125,7 +125,7 @@
125
125
  (計測結果)
126
126
  | Method | Mean | Error | StdDev | Median | Allocated |
127
127
  |-------------- |------------:|-----------:|----------:|------------:|----------:|
128
- | UseRefrection | 202.8417 ns | 13.9314 ns | 0.7636 ns | 203.0229 ns | 48 B |
128
+ | UseReflection | 202.8417 ns | 13.9314 ns | 0.7636 ns | 203.0229 ns | 48 B |
129
129
  | UseArray | 0.0055 ns | 0.1723 ns | 0.0094 ns | 0.0000 ns | - |
130
130
  | UseDictionary | 47.3129 ns | 9.5924 ns | 0.5258 ns | 47.3259 ns | 48 B |
131
131
  ```

2

修正

2021/12/01 01:52

投稿

退会済みユーザー
answer CHANGED
@@ -116,7 +116,6 @@
116
116
 
117
117
  class Program
118
118
  {
119
-
120
119
  static void Main()
121
120
  {
122
121
  BenchmarkRunner.Run<Test>();

1

追記

2021/12/01 01:25

投稿

退会済みユーザー
answer CHANGED
@@ -31,4 +31,102 @@
31
31
  Console.ReadKey();
32
32
  }
33
33
  }
34
+ ```
35
+
36
+ 文字列で値を持ってくるならDictionaryがありますし、別に文字列である必要が無いなら、配列やListもあります。
37
+ ```cs
38
+ using System;
39
+ using System.Collections.Generic;
40
+
41
+ class Program
42
+ {
43
+ private static readonly string[] _ArrayMessage =
44
+ {
45
+ "ok",
46
+ "ng",
47
+ };
48
+
49
+ private static readonly Dictionary<string, string> _DictMessage = new Dictionary<string, string>()
50
+ {
51
+ {"Message1", "ok"},
52
+ {"Message2", "ng"},
53
+ };
54
+
55
+ static void Main()
56
+ {
57
+ Console.WriteLine(_ArrayMessage[0]);
58
+ Console.WriteLine(_DictMessage["Message1"]);
59
+
60
+ Console.ReadKey();
61
+ }
62
+ }
63
+ ```
64
+
65
+ リフレクションの速度が非常に遅いと言われても、どれくらい遅いのかピンと来ないと思うので、[BenchmarkDotNet](https://github.com/dotnet/BenchmarkDotNet) の計測結果を載せておきます。
66
+
67
+ ```cs
68
+ using BenchmarkDotNet.Attributes;
69
+ using BenchmarkDotNet.Diagnosers;
70
+ using BenchmarkDotNet.Running;
71
+ using System.Collections.Generic;
72
+ using System.Reflection;
73
+
74
+ public static class Message
75
+ {
76
+ public static string Message1 { get; } = "ok";
77
+ public static string Message2 { get; } = "ng";
78
+ }
79
+
80
+ //計測用テストクラス
81
+ [MemoryDiagnoser(false)]
82
+ [ShortRunJob]
83
+ public class Test
84
+ {
85
+ private static readonly string[] _ArrayMessage =
86
+ {
87
+ "ok",
88
+ "ng",
89
+ };
90
+
91
+ private static readonly Dictionary<string, string> _DictMessage = new Dictionary<string, string>()
92
+ {
93
+ {"Message1", "ok"},
94
+ {"Message2", "ng"},
95
+ };
96
+
97
+ private int _Index = 0;
98
+ private string _StringKey = "1";
99
+
100
+ [Benchmark]
101
+ public string UseRefrection()
102
+ {
103
+ var prop = typeof(Message).GetProperty(
104
+ "Message" + _StringKey,
105
+ BindingFlags.GetProperty | BindingFlags.Static | BindingFlags.Public);
106
+
107
+ return (string)prop.GetValue(null);
108
+ }
109
+
110
+ [Benchmark]
111
+ public string UseArray() => _ArrayMessage[_Index];
112
+
113
+ [Benchmark]
114
+ public string UseDictionary() => _DictMessage["Message" + _StringKey];
115
+ }
116
+
117
+ class Program
118
+ {
119
+
120
+ static void Main()
121
+ {
122
+ BenchmarkRunner.Run<Test>();
123
+ }
124
+ }
125
+
126
+ (計測結果)
127
+ | Method | Mean | Error | StdDev | Median | Allocated |
128
+ |-------------- |------------:|-----------:|----------:|------------:|----------:|
129
+ | UseRefrection | 202.8417 ns | 13.9314 ns | 0.7636 ns | 203.0229 ns | 48 B |
130
+ | UseArray | 0.0055 ns | 0.1723 ns | 0.0094 ns | 0.0000 ns | - |
131
+ | UseDictionary | 47.3129 ns | 9.5924 ns | 0.5258 ns | 47.3259 ns | 48 B |
34
132
  ```