Guest User

Untitled

a guest
Jun 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.24 KB | None | 0 0
  1. if (a == "b" || "c")
  2.  
  3. if (a == "b" || a== "c")
  4.  
  5. if (new[] { "b", "c" }.Contains(a))
  6.  
  7. ||, not found: 26 ms
  8. ||, found: 8 ms
  9. array.Contains, not found: 1407 ms
  10. array.Contains, found: 1388 ms
  11. array.Contains, inline array, not found: 1456 ms
  12. array.Contains, inline array, found: 1427 ms
  13. switch-statement, not interned, not found: 26 ms
  14. switch-statement, not interned, found: 14 ms
  15. switch-statement, interned, not found: 25 ms
  16. switch-statement, interned, found: 8 ms
  17.  
  18. using System;
  19. using System.Linq;
  20. using System.Diagnostics;
  21. namespace StackOverflow826081
  22. {
  23. class Program
  24. {
  25. private const Int32 ITERATIONS = 1000000;
  26. static void Main()
  27. {
  28. String a;
  29. String[] ops = CreateArray();
  30. Int32 count;
  31. Stopwatch sw = new Stopwatch();
  32. Int32 pass = 0;
  33. Action<String, Int32> report = delegate(String title, Int32 i)
  34. {
  35. if (pass == 2)
  36. Console.Out.WriteLine(title + ": " + sw.ElapsedMilliseconds + " ms");
  37. };
  38.  
  39. for (pass = 1; pass <= 2; pass++)
  40. {
  41. #region || operator
  42.  
  43. a = "a";
  44. sw.Start();
  45.  
  46. count = 0;
  47. for (Int32 index = 0; index < ITERATIONS; index++)
  48. {
  49. if (a == "b" || a == "c")
  50. {
  51. count++;
  52. }
  53. }
  54. sw.Stop();
  55. report("||, not found", count);
  56. sw.Reset();
  57.  
  58. a = "b";
  59. sw.Start();
  60.  
  61. count = 0;
  62. for (Int32 index = 0; index < ITERATIONS; index++)
  63. {
  64. if (a == "b" || a == "c")
  65. {
  66. count++;
  67. }
  68. }
  69. sw.Stop();
  70. report("||, found", count);
  71. sw.Reset();
  72.  
  73. #endregion
  74.  
  75. #region array.Contains
  76.  
  77. a = "a";
  78. sw.Start();
  79.  
  80. count = 0;
  81. for (Int32 index = 0; index < ITERATIONS; index++)
  82. {
  83. if (ops.Contains(a))
  84. {
  85. count++;
  86. }
  87. }
  88. sw.Stop();
  89. report("array.Contains, not found", count);
  90. sw.Reset();
  91.  
  92. a = "b";
  93. sw.Start();
  94.  
  95. count = 0;
  96. for (Int32 index = 0; index < ITERATIONS; index++)
  97. {
  98. if (ops.Contains(a))
  99. {
  100. count++;
  101. }
  102. }
  103. sw.Stop();
  104. report("array.Contains, found", count);
  105. sw.Reset();
  106.  
  107. #endregion
  108.  
  109. #region array.Contains
  110.  
  111. a = "a";
  112. sw.Start();
  113.  
  114. count = 0;
  115. for (Int32 index = 0; index < ITERATIONS; index++)
  116. {
  117. if (CreateArray().Contains(a))
  118. {
  119. count++;
  120. }
  121. }
  122. sw.Stop();
  123. report("array.Contains, inline array, not found", count);
  124. sw.Reset();
  125.  
  126. a = "b";
  127. sw.Start();
  128.  
  129. count = 0;
  130. for (Int32 index = 0; index < ITERATIONS; index++)
  131. {
  132. if (CreateArray().Contains(a))
  133. {
  134. count++;
  135. }
  136. }
  137. sw.Stop();
  138. report("array.Contains, inline array, found", count);
  139. sw.Reset();
  140.  
  141. #endregion
  142.  
  143. #region switch-statement
  144.  
  145. a = GetString().Substring(0, 1); // avoid interned string
  146. sw.Start();
  147.  
  148. count = 0;
  149. for (Int32 index = 0; index < ITERATIONS; index++)
  150. {
  151. switch (a)
  152. {
  153. case "b":
  154. case "c":
  155. count++;
  156. break;
  157. }
  158. }
  159. sw.Stop();
  160. report("switch-statement, not interned, not found", count);
  161. sw.Reset();
  162.  
  163. a = GetString().Substring(1, 1); // avoid interned string
  164. sw.Start();
  165.  
  166. count = 0;
  167. for (Int32 index = 0; index < ITERATIONS; index++)
  168. {
  169. switch (a)
  170. {
  171. case "b":
  172. case "c":
  173. count++;
  174. break;
  175. }
  176. }
  177. sw.Stop();
  178. report("switch-statement, not interned, found", count);
  179. sw.Reset();
  180.  
  181. #endregion
  182.  
  183. #region switch-statement
  184.  
  185. a = "a";
  186. sw.Start();
  187.  
  188. count = 0;
  189. for (Int32 index = 0; index < ITERATIONS; index++)
  190. {
  191. switch (a)
  192. {
  193. case "b":
  194. case "c":
  195. count++;
  196. break;
  197. }
  198. }
  199. sw.Stop();
  200. report("switch-statement, interned, not found", count);
  201. sw.Reset();
  202.  
  203. a = "b";
  204. sw.Start();
  205.  
  206. count = 0;
  207. for (Int32 index = 0; index < ITERATIONS; index++)
  208. {
  209. switch (a)
  210. {
  211. case "b":
  212. case "c":
  213. count++;
  214. break;
  215. }
  216. }
  217. sw.Stop();
  218. report("switch-statement, interned, found", count);
  219. sw.Reset();
  220.  
  221. #endregion
  222. }
  223. }
  224.  
  225. private static String GetString()
  226. {
  227. return "ab";
  228. }
  229.  
  230. private static String[] CreateArray()
  231. {
  232. return new String[] { "b", "c" };
  233. }
  234. }
  235. }
  236.  
  237. switch (a) {
  238. case "b":
  239. case "c":
  240. // variable a is either "b" or "c"
  241. break;
  242. }
  243.  
  244. if ("bc".Contains(a)) { } // Maybe check a.Length == 1, too.
  245.  
  246. if ((a[0] & 0x62) == 0x62) { } // Maybe check a.Length == 1, too.
  247.  
  248. if (new String[] { "b", "c" }.Contains(a)) { }
  249.  
  250. if(Regex.IsMatch(a, "b|c"))
  251.  
  252. if(Regex.IsMatch(a, "^(b|c)$"))
  253.  
  254. [Flags]
  255. enum MyEnum {
  256. None = 0,
  257. A = 1,
  258. B = 2,
  259. C = 4,
  260. D = 8
  261. }
  262.  
  263. //...
  264.  
  265. MyEnum a = MyEnum.B
  266.  
  267. if((a & (MyEnum.B | MyEnum.C)) > 0)
  268. // do something
  269.  
  270. if((a & MyEnum.B) > 0 || (a & MyEnum.C) > 0)
  271. // do something
  272.  
  273. None = 00000
  274. A = 00001
  275. B = 00010
  276. C = 00100
  277. D = 01000
  278.  
  279. B 00010
  280. & C 00100
  281. ---------
  282. 00110
  283.  
  284. (B & C) 00110
  285. & (a = B) 00010
  286. ---------------
  287. 00010
  288.  
  289. public static bool Any(object a, params object[] b)
  290. {
  291. foreach(object item in b)
  292. {
  293. if(a == b)
  294. {
  295. return true;
  296. }
  297. }
  298. return false;
  299. }
Add Comment
Please, Sign In to add comment