[C#] 「世界一IQの低いソースコードはこれ。」のような if 文を書いた場合の速度を測ってみた
「世界一IQの低いソースコードはこれ。」で始まる tweet が興味深かった。
https://twitter.com/vjroba/status/494882208788660226
世界一IQの低いソースコードはこれ。
if と else を使って KeyEvent クラスの中の 200 を超える数の定数と一つずつ比較して、文字列に変換している Java のコードだ。
色々リプライされているようだが、「初心者向けの入門書なので、リフレクションや連想配列を (switch すら) 使わない方針で書いている」のかも知れない。
しかし、何のために、こんな方法を使ってまで KeyEvent の中の定数を文字列にしてるのかは不明だ。
初心者向けサンプルにこういう「実務で余り役に立たないし、手本にならないコード」を選んでいることが間違っているように思う。
C# で試した
コードの良し悪しについては議論するまでもないような気がするし、定数名を文字列に変換する必要性が生じた時点でおかしい気がするが、if をこんなに続けた場合のパフォーマンスが気になったので、C# で試した。
以下では、あえて if を使って定数名を文字列に変換し、switch や Dictinary などを使った場合と処理時間を比較してみたい。
Sample クラス
先ず、225 個の const int を持つ Sample クラス。あえて enum は使っていない。
※ ぞっとするようなこのクラスや後述する else if の繰り返し, case の繰り返しのソースコードは、Enumerable.Range(1,225).ToList().ForEach(number => Console.WriteLine(適当なフォーマット文字列, number)) で作った。
public static class Sample { public const int CONST_001 = 1; public const int CONST_002 = 2; public const int CONST_003 = 3; public const int CONST_004 = 4; public const int CONST_005 = 5; public const int CONST_006 = 6; public const int CONST_007 = 7; public const int CONST_008 = 8; public const int CONST_009 = 9; public const int CONST_010 = 10; public const int CONST_011 = 11; public const int CONST_012 = 12; public const int CONST_013 = 13; public const int CONST_014 = 14; public const int CONST_015 = 15; public const int CONST_016 = 16; public const int CONST_017 = 17; public const int CONST_018 = 18; public const int CONST_019 = 19; public const int CONST_020 = 20; public const int CONST_021 = 21; public const int CONST_022 = 22; public const int CONST_023 = 23; public const int CONST_024 = 24; public const int CONST_025 = 25; public const int CONST_026 = 26; public const int CONST_027 = 27; public const int CONST_028 = 28; public const int CONST_029 = 29; public const int CONST_030 = 30; public const int CONST_031 = 31; public const int CONST_032 = 32; public const int CONST_033 = 33; public const int CONST_034 = 34; public const int CONST_035 = 35; public const int CONST_036 = 36; public const int CONST_037 = 37; public const int CONST_038 = 38; public const int CONST_039 = 39; public const int CONST_040 = 40; public const int CONST_041 = 41; public const int CONST_042 = 42; public const int CONST_043 = 43; public const int CONST_044 = 44; public const int CONST_045 = 45; public const int CONST_046 = 46; public const int CONST_047 = 47; public const int CONST_048 = 48; public const int CONST_049 = 49; public const int CONST_050 = 50; public const int CONST_051 = 51; public const int CONST_052 = 52; public const int CONST_053 = 53; public const int CONST_054 = 54; public const int CONST_055 = 55; public const int CONST_056 = 56; public const int CONST_057 = 57; public const int CONST_058 = 58; public const int CONST_059 = 59; public const int CONST_060 = 60; public const int CONST_061 = 61; public const int CONST_062 = 62; public const int CONST_063 = 63; public const int CONST_064 = 64; public const int CONST_065 = 65; public const int CONST_066 = 66; public const int CONST_067 = 67; public const int CONST_068 = 68; public const int CONST_069 = 69; public const int CONST_070 = 70; public const int CONST_071 = 71; public const int CONST_072 = 72; public const int CONST_073 = 73; public const int CONST_074 = 74; public const int CONST_075 = 75; public const int CONST_076 = 76; public const int CONST_077 = 77; public const int CONST_078 = 78; public const int CONST_079 = 79; public const int CONST_080 = 80; public const int CONST_081 = 81; public const int CONST_082 = 82; public const int CONST_083 = 83; public const int CONST_084 = 84; public const int CONST_085 = 85; public const int CONST_086 = 86; public const int CONST_087 = 87; public const int CONST_088 = 88; public const int CONST_089 = 89; public const int CONST_090 = 90; public const int CONST_091 = 91; public const int CONST_092 = 92; public const int CONST_093 = 93; public const int CONST_094 = 94; public const int CONST_095 = 95; public const int CONST_096 = 96; public const int CONST_097 = 97; public const int CONST_098 = 98; public const int CONST_099 = 99; public const int CONST_100 = 100; public const int CONST_101 = 101; public const int CONST_102 = 102; public const int CONST_103 = 103; public const int CONST_104 = 104; public const int CONST_105 = 105; public const int CONST_106 = 106; public const int CONST_107 = 107; public const int CONST_108 = 108; public const int CONST_109 = 109; public const int CONST_110 = 110; public const int CONST_111 = 111; public const int CONST_112 = 112; public const int CONST_113 = 113; public const int CONST_114 = 114; public const int CONST_115 = 115; public const int CONST_116 = 116; public const int CONST_117 = 117; public const int CONST_118 = 118; public const int CONST_119 = 119; public const int CONST_120 = 120; public const int CONST_121 = 121; public const int CONST_122 = 122; public const int CONST_123 = 123; public const int CONST_124 = 124; public const int CONST_125 = 125; public const int CONST_126 = 126; public const int CONST_127 = 127; public const int CONST_128 = 128; public const int CONST_129 = 129; public const int CONST_130 = 130; public const int CONST_131 = 131; public const int CONST_132 = 132; public const int CONST_133 = 133; public const int CONST_134 = 134; public const int CONST_135 = 135; public const int CONST_136 = 136; public const int CONST_137 = 137; public const int CONST_138 = 138; public const int CONST_139 = 139; public const int CONST_140 = 140; public const int CONST_141 = 141; public const int CONST_142 = 142; public const int CONST_143 = 143; public const int CONST_144 = 144; public const int CONST_145 = 145; public const int CONST_146 = 146; public const int CONST_147 = 147; public const int CONST_148 = 148; public const int CONST_149 = 149; public const int CONST_150 = 150; public const int CONST_151 = 151; public const int CONST_152 = 152; public const int CONST_153 = 153; public const int CONST_154 = 154; public const int CONST_155 = 155; public const int CONST_156 = 156; public const int CONST_157 = 157; public const int CONST_158 = 158; public const int CONST_159 = 159; public const int CONST_160 = 160; public const int CONST_161 = 161; public const int CONST_162 = 162; public const int CONST_163 = 163; public const int CONST_164 = 164; public const int CONST_165 = 165; public const int CONST_166 = 166; public const int CONST_167 = 167; public const int CONST_168 = 168; public const int CONST_169 = 169; public const int CONST_170 = 170; public const int CONST_171 = 171; public const int CONST_172 = 172; public const int CONST_173 = 173; public const int CONST_174 = 174; public const int CONST_175 = 175; public const int CONST_176 = 176; public const int CONST_177 = 177; public const int CONST_178 = 178; public const int CONST_179 = 179; public const int CONST_180 = 180; public const int CONST_181 = 181; public const int CONST_182 = 182; public const int CONST_183 = 183; public const int CONST_184 = 184; public const int CONST_185 = 185; public const int CONST_186 = 186; public const int CONST_187 = 187; public const int CONST_188 = 188; public const int CONST_189 = 189; public const int CONST_190 = 190; public const int CONST_191 = 191; public const int CONST_192 = 192; public const int CONST_193 = 193; public const int CONST_194 = 194; public const int CONST_195 = 195; public const int CONST_196 = 196; public const int CONST_197 = 197; public const int CONST_198 = 198; public const int CONST_199 = 199; public const int CONST_200 = 200; public const int CONST_201 = 201; public const int CONST_202 = 202; public const int CONST_203 = 203; public const int CONST_204 = 204; public const int CONST_205 = 205; public const int CONST_206 = 206; public const int CONST_207 = 207; public const int CONST_208 = 208; public const int CONST_209 = 209; public const int CONST_210 = 210; public const int CONST_211 = 211; public const int CONST_212 = 212; public const int CONST_213 = 213; public const int CONST_214 = 214; public const int CONST_215 = 215; public const int CONST_216 = 216; public const int CONST_217 = 217; public const int CONST_218 = 218; public const int CONST_219 = 219; public const int CONST_220 = 220; public const int CONST_221 = 221; public const int CONST_222 = 222; public const int CONST_223 = 223; public const int CONST_224 = 224; public const int CONST_225 = 225; }
PerformanceTester クラス
次に、実行時間測定用に PerformanceTester というクラスを用意した。
using System; using System.Diagnostics; public static class PerformanceTester { static Stopwatch stopwatch = new Stopwatch(); public static double Test(Action action, long times) { stopwatch.Restart(); for (var counter = 0L; counter < times; counter++) action(); stopwatch.Stop(); return stopwatch.ElapsedMilliseconds / 1000.0; } }
IfPerformanceTestProgram クラス
最後に Main を含んだクラスだ。
このクラスには、次の3種の「int の値を渡すと定数名を返す」メソッドがある。
- tweet されていた書籍にあったように、if と else で書いた IfSample
- switch – case で書いた SwitchSample
- Dictionary で書いた ReflectionAndDictionarySample
最後の Dictionary で書いたものは、初期化の部分にリフレクションを使った ConstantsTable クラスを使っている。
毎回リフレクションで取得することも可能だが、そうすると、3桁位実行速度が落ちてしまう。
そこで、最初にリフレクションで、フィールドの値と名前の一覧を取得し、値をキーとして Dictionary に格納する。
その Dictionary を使って、値から文字列を得る仕組みだ。
Test メソッドは、「渡されたメソッドを使って1~255の値を定数名に変換する」処理を 100,000回実行する時間を測り、それを表示する。
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; class IfPerformanceTestProgram { static string IfSample(int value) { string result; if (value == Sample.CONST_001) result = "CONST_001"; else if (value == Sample.CONST_002) result = "CONST_002"; else if (value == Sample.CONST_003) result = "CONST_003"; else if (value == Sample.CONST_004) result = "CONST_004"; else if (value == Sample.CONST_005) result = "CONST_005"; else if (value == Sample.CONST_006) result = "CONST_006"; else if (value == Sample.CONST_007) result = "CONST_007"; else if (value == Sample.CONST_008) result = "CONST_008"; else if (value == Sample.CONST_009) result = "CONST_009"; else if (value == Sample.CONST_010) result = "CONST_010"; else if (value == Sample.CONST_011) result = "CONST_011"; else if (value == Sample.CONST_012) result = "CONST_012"; else if (value == Sample.CONST_013) result = "CONST_013"; else if (value == Sample.CONST_014) result = "CONST_014"; else if (value == Sample.CONST_015) result = "CONST_015"; else if (value == Sample.CONST_016) result = "CONST_016"; else if (value == Sample.CONST_017) result = "CONST_017"; else if (value == Sample.CONST_018) result = "CONST_018"; else if (value == Sample.CONST_019) result = "CONST_019"; else if (value == Sample.CONST_020) result = "CONST_020"; else if (value == Sample.CONST_021) result = "CONST_021"; else if (value == Sample.CONST_022) result = "CONST_022"; else if (value == Sample.CONST_023) result = "CONST_023"; else if (value == Sample.CONST_024) result = "CONST_024"; else if (value == Sample.CONST_025) result = "CONST_025"; else if (value == Sample.CONST_026) result = "CONST_026"; else if (value == Sample.CONST_027) result = "CONST_027"; else if (value == Sample.CONST_028) result = "CONST_028"; else if (value == Sample.CONST_029) result = "CONST_029"; else if (value == Sample.CONST_030) result = "CONST_030"; else if (value == Sample.CONST_031) result = "CONST_031"; else if (value == Sample.CONST_032) result = "CONST_032"; else if (value == Sample.CONST_033) result = "CONST_033"; else if (value == Sample.CONST_034) result = "CONST_034"; else if (value == Sample.CONST_035) result = "CONST_035"; else if (value == Sample.CONST_036) result = "CONST_036"; else if (value == Sample.CONST_037) result = "CONST_037"; else if (value == Sample.CONST_038) result = "CONST_038"; else if (value == Sample.CONST_039) result = "CONST_039"; else if (value == Sample.CONST_040) result = "CONST_040"; else if (value == Sample.CONST_041) result = "CONST_041"; else if (value == Sample.CONST_042) result = "CONST_042"; else if (value == Sample.CONST_043) result = "CONST_043"; else if (value == Sample.CONST_044) result = "CONST_044"; else if (value == Sample.CONST_045) result = "CONST_045"; else if (value == Sample.CONST_046) result = "CONST_046"; else if (value == Sample.CONST_047) result = "CONST_047"; else if (value == Sample.CONST_048) result = "CONST_048"; else if (value == Sample.CONST_049) result = "CONST_049"; else if (value == Sample.CONST_050) result = "CONST_050"; else if (value == Sample.CONST_051) result = "CONST_051"; else if (value == Sample.CONST_052) result = "CONST_052"; else if (value == Sample.CONST_053) result = "CONST_053"; else if (value == Sample.CONST_054) result = "CONST_054"; else if (value == Sample.CONST_055) result = "CONST_055"; else if (value == Sample.CONST_056) result = "CONST_056"; else if (value == Sample.CONST_057) result = "CONST_057"; else if (value == Sample.CONST_058) result = "CONST_058"; else if (value == Sample.CONST_059) result = "CONST_059"; else if (value == Sample.CONST_060) result = "CONST_060"; else if (value == Sample.CONST_061) result = "CONST_061"; else if (value == Sample.CONST_062) result = "CONST_062"; else if (value == Sample.CONST_063) result = "CONST_063"; else if (value == Sample.CONST_064) result = "CONST_064"; else if (value == Sample.CONST_065) result = "CONST_065"; else if (value == Sample.CONST_066) result = "CONST_066"; else if (value == Sample.CONST_067) result = "CONST_067"; else if (value == Sample.CONST_068) result = "CONST_068"; else if (value == Sample.CONST_069) result = "CONST_069"; else if (value == Sample.CONST_070) result = "CONST_070"; else if (value == Sample.CONST_071) result = "CONST_071"; else if (value == Sample.CONST_072) result = "CONST_072"; else if (value == Sample.CONST_073) result = "CONST_073"; else if (value == Sample.CONST_074) result = "CONST_074"; else if (value == Sample.CONST_075) result = "CONST_075"; else if (value == Sample.CONST_076) result = "CONST_076"; else if (value == Sample.CONST_077) result = "CONST_077"; else if (value == Sample.CONST_078) result = "CONST_078"; else if (value == Sample.CONST_079) result = "CONST_079"; else if (value == Sample.CONST_080) result = "CONST_080"; else if (value == Sample.CONST_081) result = "CONST_081"; else if (value == Sample.CONST_082) result = "CONST_082"; else if (value == Sample.CONST_083) result = "CONST_083"; else if (value == Sample.CONST_084) result = "CONST_084"; else if (value == Sample.CONST_085) result = "CONST_085"; else if (value == Sample.CONST_086) result = "CONST_086"; else if (value == Sample.CONST_087) result = "CONST_087"; else if (value == Sample.CONST_088) result = "CONST_088"; else if (value == Sample.CONST_089) result = "CONST_089"; else if (value == Sample.CONST_090) result = "CONST_090"; else if (value == Sample.CONST_091) result = "CONST_091"; else if (value == Sample.CONST_092) result = "CONST_092"; else if (value == Sample.CONST_093) result = "CONST_093"; else if (value == Sample.CONST_094) result = "CONST_094"; else if (value == Sample.CONST_095) result = "CONST_095"; else if (value == Sample.CONST_096) result = "CONST_096"; else if (value == Sample.CONST_097) result = "CONST_097"; else if (value == Sample.CONST_098) result = "CONST_098"; else if (value == Sample.CONST_099) result = "CONST_099"; else if (value == Sample.CONST_100) result = "CONST_100"; else if (value == Sample.CONST_101) result = "CONST_101"; else if (value == Sample.CONST_102) result = "CONST_102"; else if (value == Sample.CONST_103) result = "CONST_103"; else if (value == Sample.CONST_104) result = "CONST_104"; else if (value == Sample.CONST_105) result = "CONST_105"; else if (value == Sample.CONST_106) result = "CONST_106"; else if (value == Sample.CONST_107) result = "CONST_107"; else if (value == Sample.CONST_108) result = "CONST_108"; else if (value == Sample.CONST_109) result = "CONST_109"; else if (value == Sample.CONST_110) result = "CONST_110"; else if (value == Sample.CONST_111) result = "CONST_111"; else if (value == Sample.CONST_112) result = "CONST_112"; else if (value == Sample.CONST_113) result = "CONST_113"; else if (value == Sample.CONST_114) result = "CONST_114"; else if (value == Sample.CONST_115) result = "CONST_115"; else if (value == Sample.CONST_116) result = "CONST_116"; else if (value == Sample.CONST_117) result = "CONST_117"; else if (value == Sample.CONST_118) result = "CONST_118"; else if (value == Sample.CONST_119) result = "CONST_119"; else if (value == Sample.CONST_120) result = "CONST_120"; else if (value == Sample.CONST_121) result = "CONST_121"; else if (value == Sample.CONST_122) result = "CONST_122"; else if (value == Sample.CONST_123) result = "CONST_123"; else if (value == Sample.CONST_124) result = "CONST_124"; else if (value == Sample.CONST_125) result = "CONST_125"; else if (value == Sample.CONST_126) result = "CONST_126"; else if (value == Sample.CONST_127) result = "CONST_127"; else if (value == Sample.CONST_128) result = "CONST_128"; else if (value == Sample.CONST_129) result = "CONST_129"; else if (value == Sample.CONST_130) result = "CONST_130"; else if (value == Sample.CONST_131) result = "CONST_131"; else if (value == Sample.CONST_132) result = "CONST_132"; else if (value == Sample.CONST_133) result = "CONST_133"; else if (value == Sample.CONST_134) result = "CONST_134"; else if (value == Sample.CONST_135) result = "CONST_135"; else if (value == Sample.CONST_136) result = "CONST_136"; else if (value == Sample.CONST_137) result = "CONST_137"; else if (value == Sample.CONST_138) result = "CONST_138"; else if (value == Sample.CONST_139) result = "CONST_139"; else if (value == Sample.CONST_140) result = "CONST_140"; else if (value == Sample.CONST_141) result = "CONST_141"; else if (value == Sample.CONST_142) result = "CONST_142"; else if (value == Sample.CONST_143) result = "CONST_143"; else if (value == Sample.CONST_144) result = "CONST_144"; else if (value == Sample.CONST_145) result = "CONST_145"; else if (value == Sample.CONST_146) result = "CONST_146"; else if (value == Sample.CONST_147) result = "CONST_147"; else if (value == Sample.CONST_148) result = "CONST_148"; else if (value == Sample.CONST_149) result = "CONST_149"; else if (value == Sample.CONST_150) result = "CONST_150"; else if (value == Sample.CONST_151) result = "CONST_151"; else if (value == Sample.CONST_152) result = "CONST_152"; else if (value == Sample.CONST_153) result = "CONST_153"; else if (value == Sample.CONST_154) result = "CONST_154"; else if (value == Sample.CONST_155) result = "CONST_155"; else if (value == Sample.CONST_156) result = "CONST_156"; else if (value == Sample.CONST_157) result = "CONST_157"; else if (value == Sample.CONST_158) result = "CONST_158"; else if (value == Sample.CONST_159) result = "CONST_159"; else if (value == Sample.CONST_160) result = "CONST_160"; else if (value == Sample.CONST_161) result = "CONST_161"; else if (value == Sample.CONST_162) result = "CONST_162"; else if (value == Sample.CONST_163) result = "CONST_163"; else if (value == Sample.CONST_164) result = "CONST_164"; else if (value == Sample.CONST_165) result = "CONST_165"; else if (value == Sample.CONST_166) result = "CONST_166"; else if (value == Sample.CONST_167) result = "CONST_167"; else if (value == Sample.CONST_168) result = "CONST_168"; else if (value == Sample.CONST_169) result = "CONST_169"; else if (value == Sample.CONST_170) result = "CONST_170"; else if (value == Sample.CONST_171) result = "CONST_171"; else if (value == Sample.CONST_172) result = "CONST_172"; else if (value == Sample.CONST_173) result = "CONST_173"; else if (value == Sample.CONST_174) result = "CONST_174"; else if (value == Sample.CONST_175) result = "CONST_175"; else if (value == Sample.CONST_176) result = "CONST_176"; else if (value == Sample.CONST_177) result = "CONST_177"; else if (value == Sample.CONST_178) result = "CONST_178"; else if (value == Sample.CONST_179) result = "CONST_179"; else if (value == Sample.CONST_180) result = "CONST_180"; else if (value == Sample.CONST_181) result = "CONST_181"; else if (value == Sample.CONST_182) result = "CONST_182"; else if (value == Sample.CONST_183) result = "CONST_183"; else if (value == Sample.CONST_184) result = "CONST_184"; else if (value == Sample.CONST_185) result = "CONST_185"; else if (value == Sample.CONST_186) result = "CONST_186"; else if (value == Sample.CONST_187) result = "CONST_187"; else if (value == Sample.CONST_188) result = "CONST_188"; else if (value == Sample.CONST_189) result = "CONST_189"; else if (value == Sample.CONST_190) result = "CONST_190"; else if (value == Sample.CONST_191) result = "CONST_191"; else if (value == Sample.CONST_192) result = "CONST_192"; else if (value == Sample.CONST_193) result = "CONST_193"; else if (value == Sample.CONST_194) result = "CONST_194"; else if (value == Sample.CONST_195) result = "CONST_195"; else if (value == Sample.CONST_196) result = "CONST_196"; else if (value == Sample.CONST_197) result = "CONST_197"; else if (value == Sample.CONST_198) result = "CONST_198"; else if (value == Sample.CONST_199) result = "CONST_199"; else if (value == Sample.CONST_200) result = "CONST_200"; else if (value == Sample.CONST_201) result = "CONST_201"; else if (value == Sample.CONST_202) result = "CONST_202"; else if (value == Sample.CONST_203) result = "CONST_203"; else if (value == Sample.CONST_204) result = "CONST_204"; else if (value == Sample.CONST_205) result = "CONST_205"; else if (value == Sample.CONST_206) result = "CONST_206"; else if (value == Sample.CONST_207) result = "CONST_207"; else if (value == Sample.CONST_208) result = "CONST_208"; else if (value == Sample.CONST_209) result = "CONST_209"; else if (value == Sample.CONST_210) result = "CONST_210"; else if (value == Sample.CONST_211) result = "CONST_211"; else if (value == Sample.CONST_212) result = "CONST_212"; else if (value == Sample.CONST_213) result = "CONST_213"; else if (value == Sample.CONST_214) result = "CONST_214"; else if (value == Sample.CONST_215) result = "CONST_215"; else if (value == Sample.CONST_216) result = "CONST_216"; else if (value == Sample.CONST_217) result = "CONST_217"; else if (value == Sample.CONST_218) result = "CONST_218"; else if (value == Sample.CONST_219) result = "CONST_219"; else if (value == Sample.CONST_220) result = "CONST_220"; else if (value == Sample.CONST_221) result = "CONST_221"; else if (value == Sample.CONST_222) result = "CONST_222"; else if (value == Sample.CONST_223) result = "CONST_223"; else if (value == Sample.CONST_224) result = "CONST_224"; else if (value == Sample.CONST_225) result = "CONST_225"; else result = "" ; return result; } static string SwitchSample(int value) { string result; switch (value) { case Sample.CONST_001: result = "CONST_001"; break; case Sample.CONST_002: result = "CONST_002"; break; case Sample.CONST_003: result = "CONST_003"; break; case Sample.CONST_004: result = "CONST_004"; break; case Sample.CONST_005: result = "CONST_005"; break; case Sample.CONST_006: result = "CONST_006"; break; case Sample.CONST_007: result = "CONST_007"; break; case Sample.CONST_008: result = "CONST_008"; break; case Sample.CONST_009: result = "CONST_009"; break; case Sample.CONST_010: result = "CONST_010"; break; case Sample.CONST_011: result = "CONST_011"; break; case Sample.CONST_012: result = "CONST_012"; break; case Sample.CONST_013: result = "CONST_013"; break; case Sample.CONST_014: result = "CONST_014"; break; case Sample.CONST_015: result = "CONST_015"; break; case Sample.CONST_016: result = "CONST_016"; break; case Sample.CONST_017: result = "CONST_017"; break; case Sample.CONST_018: result = "CONST_018"; break; case Sample.CONST_019: result = "CONST_019"; break; case Sample.CONST_020: result = "CONST_020"; break; case Sample.CONST_021: result = "CONST_021"; break; case Sample.CONST_022: result = "CONST_022"; break; case Sample.CONST_023: result = "CONST_023"; break; case Sample.CONST_024: result = "CONST_024"; break; case Sample.CONST_025: result = "CONST_025"; break; case Sample.CONST_026: result = "CONST_026"; break; case Sample.CONST_027: result = "CONST_027"; break; case Sample.CONST_028: result = "CONST_028"; break; case Sample.CONST_029: result = "CONST_029"; break; case Sample.CONST_030: result = "CONST_030"; break; case Sample.CONST_031: result = "CONST_031"; break; case Sample.CONST_032: result = "CONST_032"; break; case Sample.CONST_033: result = "CONST_033"; break; case Sample.CONST_034: result = "CONST_034"; break; case Sample.CONST_035: result = "CONST_035"; break; case Sample.CONST_036: result = "CONST_036"; break; case Sample.CONST_037: result = "CONST_037"; break; case Sample.CONST_038: result = "CONST_038"; break; case Sample.CONST_039: result = "CONST_039"; break; case Sample.CONST_040: result = "CONST_040"; break; case Sample.CONST_041: result = "CONST_041"; break; case Sample.CONST_042: result = "CONST_042"; break; case Sample.CONST_043: result = "CONST_043"; break; case Sample.CONST_044: result = "CONST_044"; break; case Sample.CONST_045: result = "CONST_045"; break; case Sample.CONST_046: result = "CONST_046"; break; case Sample.CONST_047: result = "CONST_047"; break; case Sample.CONST_048: result = "CONST_048"; break; case Sample.CONST_049: result = "CONST_049"; break; case Sample.CONST_050: result = "CONST_050"; break; case Sample.CONST_051: result = "CONST_051"; break; case Sample.CONST_052: result = "CONST_052"; break; case Sample.CONST_053: result = "CONST_053"; break; case Sample.CONST_054: result = "CONST_054"; break; case Sample.CONST_055: result = "CONST_055"; break; case Sample.CONST_056: result = "CONST_056"; break; case Sample.CONST_057: result = "CONST_057"; break; case Sample.CONST_058: result = "CONST_058"; break; case Sample.CONST_059: result = "CONST_059"; break; case Sample.CONST_060: result = "CONST_060"; break; case Sample.CONST_061: result = "CONST_061"; break; case Sample.CONST_062: result = "CONST_062"; break; case Sample.CONST_063: result = "CONST_063"; break; case Sample.CONST_064: result = "CONST_064"; break; case Sample.CONST_065: result = "CONST_065"; break; case Sample.CONST_066: result = "CONST_066"; break; case Sample.CONST_067: result = "CONST_067"; break; case Sample.CONST_068: result = "CONST_068"; break; case Sample.CONST_069: result = "CONST_069"; break; case Sample.CONST_070: result = "CONST_070"; break; case Sample.CONST_071: result = "CONST_071"; break; case Sample.CONST_072: result = "CONST_072"; break; case Sample.CONST_073: result = "CONST_073"; break; case Sample.CONST_074: result = "CONST_074"; break; case Sample.CONST_075: result = "CONST_075"; break; case Sample.CONST_076: result = "CONST_076"; break; case Sample.CONST_077: result = "CONST_077"; break; case Sample.CONST_078: result = "CONST_078"; break; case Sample.CONST_079: result = "CONST_079"; break; case Sample.CONST_080: result = "CONST_080"; break; case Sample.CONST_081: result = "CONST_081"; break; case Sample.CONST_082: result = "CONST_082"; break; case Sample.CONST_083: result = "CONST_083"; break; case Sample.CONST_084: result = "CONST_084"; break; case Sample.CONST_085: result = "CONST_085"; break; case Sample.CONST_086: result = "CONST_086"; break; case Sample.CONST_087: result = "CONST_087"; break; case Sample.CONST_088: result = "CONST_088"; break; case Sample.CONST_089: result = "CONST_089"; break; case Sample.CONST_090: result = "CONST_090"; break; case Sample.CONST_091: result = "CONST_091"; break; case Sample.CONST_092: result = "CONST_092"; break; case Sample.CONST_093: result = "CONST_093"; break; case Sample.CONST_094: result = "CONST_094"; break; case Sample.CONST_095: result = "CONST_095"; break; case Sample.CONST_096: result = "CONST_096"; break; case Sample.CONST_097: result = "CONST_097"; break; case Sample.CONST_098: result = "CONST_098"; break; case Sample.CONST_099: result = "CONST_099"; break; case Sample.CONST_100: result = "CONST_100"; break; case Sample.CONST_101: result = "CONST_101"; break; case Sample.CONST_102: result = "CONST_102"; break; case Sample.CONST_103: result = "CONST_103"; break; case Sample.CONST_104: result = "CONST_104"; break; case Sample.CONST_105: result = "CONST_105"; break; case Sample.CONST_106: result = "CONST_106"; break; case Sample.CONST_107: result = "CONST_107"; break; case Sample.CONST_108: result = "CONST_108"; break; case Sample.CONST_109: result = "CONST_109"; break; case Sample.CONST_110: result = "CONST_110"; break; case Sample.CONST_111: result = "CONST_111"; break; case Sample.CONST_112: result = "CONST_112"; break; case Sample.CONST_113: result = "CONST_113"; break; case Sample.CONST_114: result = "CONST_114"; break; case Sample.CONST_115: result = "CONST_115"; break; case Sample.CONST_116: result = "CONST_116"; break; case Sample.CONST_117: result = "CONST_117"; break; case Sample.CONST_118: result = "CONST_118"; break; case Sample.CONST_119: result = "CONST_119"; break; case Sample.CONST_120: result = "CONST_120"; break; case Sample.CONST_121: result = "CONST_121"; break; case Sample.CONST_122: result = "CONST_122"; break; case Sample.CONST_123: result = "CONST_123"; break; case Sample.CONST_124: result = "CONST_124"; break; case Sample.CONST_125: result = "CONST_125"; break; case Sample.CONST_126: result = "CONST_126"; break; case Sample.CONST_127: result = "CONST_127"; break; case Sample.CONST_128: result = "CONST_128"; break; case Sample.CONST_129: result = "CONST_129"; break; case Sample.CONST_130: result = "CONST_130"; break; case Sample.CONST_131: result = "CONST_131"; break; case Sample.CONST_132: result = "CONST_132"; break; case Sample.CONST_133: result = "CONST_133"; break; case Sample.CONST_134: result = "CONST_134"; break; case Sample.CONST_135: result = "CONST_135"; break; case Sample.CONST_136: result = "CONST_136"; break; case Sample.CONST_137: result = "CONST_137"; break; case Sample.CONST_138: result = "CONST_138"; break; case Sample.CONST_139: result = "CONST_139"; break; case Sample.CONST_140: result = "CONST_140"; break; case Sample.CONST_141: result = "CONST_141"; break; case Sample.CONST_142: result = "CONST_142"; break; case Sample.CONST_143: result = "CONST_143"; break; case Sample.CONST_144: result = "CONST_144"; break; case Sample.CONST_145: result = "CONST_145"; break; case Sample.CONST_146: result = "CONST_146"; break; case Sample.CONST_147: result = "CONST_147"; break; case Sample.CONST_148: result = "CONST_148"; break; case Sample.CONST_149: result = "CONST_149"; break; case Sample.CONST_150: result = "CONST_150"; break; case Sample.CONST_151: result = "CONST_151"; break; case Sample.CONST_152: result = "CONST_152"; break; case Sample.CONST_153: result = "CONST_153"; break; case Sample.CONST_154: result = "CONST_154"; break; case Sample.CONST_155: result = "CONST_155"; break; case Sample.CONST_156: result = "CONST_156"; break; case Sample.CONST_157: result = "CONST_157"; break; case Sample.CONST_158: result = "CONST_158"; break; case Sample.CONST_159: result = "CONST_159"; break; case Sample.CONST_160: result = "CONST_160"; break; case Sample.CONST_161: result = "CONST_161"; break; case Sample.CONST_162: result = "CONST_162"; break; case Sample.CONST_163: result = "CONST_163"; break; case Sample.CONST_164: result = "CONST_164"; break; case Sample.CONST_165: result = "CONST_165"; break; case Sample.CONST_166: result = "CONST_166"; break; case Sample.CONST_167: result = "CONST_167"; break; case Sample.CONST_168: result = "CONST_168"; break; case Sample.CONST_169: result = "CONST_169"; break; case Sample.CONST_170: result = "CONST_170"; break; case Sample.CONST_171: result = "CONST_171"; break; case Sample.CONST_172: result = "CONST_172"; break; case Sample.CONST_173: result = "CONST_173"; break; case Sample.CONST_174: result = "CONST_174"; break; case Sample.CONST_175: result = "CONST_175"; break; case Sample.CONST_176: result = "CONST_176"; break; case Sample.CONST_177: result = "CONST_177"; break; case Sample.CONST_178: result = "CONST_178"; break; case Sample.CONST_179: result = "CONST_179"; break; case Sample.CONST_180: result = "CONST_180"; break; case Sample.CONST_181: result = "CONST_181"; break; case Sample.CONST_182: result = "CONST_182"; break; case Sample.CONST_183: result = "CONST_183"; break; case Sample.CONST_184: result = "CONST_184"; break; case Sample.CONST_185: result = "CONST_185"; break; case Sample.CONST_186: result = "CONST_186"; break; case Sample.CONST_187: result = "CONST_187"; break; case Sample.CONST_188: result = "CONST_188"; break; case Sample.CONST_189: result = "CONST_189"; break; case Sample.CONST_190: result = "CONST_190"; break; case Sample.CONST_191: result = "CONST_191"; break; case Sample.CONST_192: result = "CONST_192"; break; case Sample.CONST_193: result = "CONST_193"; break; case Sample.CONST_194: result = "CONST_194"; break; case Sample.CONST_195: result = "CONST_195"; break; case Sample.CONST_196: result = "CONST_196"; break; case Sample.CONST_197: result = "CONST_197"; break; case Sample.CONST_198: result = "CONST_198"; break; case Sample.CONST_199: result = "CONST_199"; break; case Sample.CONST_200: result = "CONST_200"; break; case Sample.CONST_201: result = "CONST_201"; break; case Sample.CONST_202: result = "CONST_202"; break; case Sample.CONST_203: result = "CONST_203"; break; case Sample.CONST_204: result = "CONST_204"; break; case Sample.CONST_205: result = "CONST_205"; break; case Sample.CONST_206: result = "CONST_206"; break; case Sample.CONST_207: result = "CONST_207"; break; case Sample.CONST_208: result = "CONST_208"; break; case Sample.CONST_209: result = "CONST_209"; break; case Sample.CONST_210: result = "CONST_210"; break; case Sample.CONST_211: result = "CONST_211"; break; case Sample.CONST_212: result = "CONST_212"; break; case Sample.CONST_213: result = "CONST_213"; break; case Sample.CONST_214: result = "CONST_214"; break; case Sample.CONST_215: result = "CONST_215"; break; case Sample.CONST_216: result = "CONST_216"; break; case Sample.CONST_217: result = "CONST_217"; break; case Sample.CONST_218: result = "CONST_218"; break; case Sample.CONST_219: result = "CONST_219"; break; case Sample.CONST_220: result = "CONST_220"; break; case Sample.CONST_221: result = "CONST_221"; break; case Sample.CONST_222: result = "CONST_222"; break; case Sample.CONST_223: result = "CONST_223"; break; case Sample.CONST_224: result = "CONST_224"; break; case Sample.CONST_225: result = "CONST_225"; break; default : result = "" ; break; } return result; } static class ConstantsTable { static Dictionary<int, string> constantsDictionary = new Dictionary<int, string>(); static ConstantsTable() { var fields = typeof(Sample) .GetFields(BindingFlags.Public | BindingFlags.Static); fields.Where(field => field.IsLiteral && field.FieldType.Equals(typeof(int))) .ToList() .ForEach(field => constantsDictionary.Add((int)field.GetValue(null), field.Name)); } public static string GetConstantName(int value) { string name; return constantsDictionary.TryGetValue(value, out name) ? name : ""; } } static string ReflectionAndDictionarySample(int value) { return ConstantsTable.GetConstantName(value); } static void Test(Func<int, string> sampleFunction, string sampleName) { const long times = 100000L; string result; var time = PerformanceTester.Test( () => Enumerable .Range(1, 225) .ToList() .ForEach(number => result = sampleFunction(number)), times ); Console.WriteLine("{0}の場合\t: {1:F3}/{2}sec.", sampleName, time, times); } static void Main() { Test(IfSample , "if" ); Test(SwitchSample , "switch" ); Test(ReflectionAndDictionarySample, "ReflectionとDictionary"); } }
実行結果
実行してみると、次のようになった。
ifの場合 : 2.575/100000sec. switchの場合 : 0.864/100000sec. ReflectionとDictionaryの場合 : 1.152/100000sec.
現状、比較が 200 以上も続く上記のようなケースでは、if と else で書いた方が遅いようだ。
ディスカッション
コメント一覧
まだ、コメントがありません