Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if (a == "b" || "c")
- if (a == "b" || a== "c")
- if (new[] { "b", "c" }.Contains(a))
- ||, not found: 26 ms
- ||, found: 8 ms
- array.Contains, not found: 1407 ms
- array.Contains, found: 1388 ms
- array.Contains, inline array, not found: 1456 ms
- array.Contains, inline array, found: 1427 ms
- switch-statement, not interned, not found: 26 ms
- switch-statement, not interned, found: 14 ms
- switch-statement, interned, not found: 25 ms
- switch-statement, interned, found: 8 ms
- using System;
- using System.Linq;
- using System.Diagnostics;
- namespace StackOverflow826081
- {
- class Program
- {
- private const Int32 ITERATIONS = 1000000;
- static void Main()
- {
- String a;
- String[] ops = CreateArray();
- Int32 count;
- Stopwatch sw = new Stopwatch();
- Int32 pass = 0;
- Action<String, Int32> report = delegate(String title, Int32 i)
- {
- if (pass == 2)
- Console.Out.WriteLine(title + ": " + sw.ElapsedMilliseconds + " ms");
- };
- for (pass = 1; pass <= 2; pass++)
- {
- #region || operator
- a = "a";
- sw.Start();
- count = 0;
- for (Int32 index = 0; index < ITERATIONS; index++)
- {
- if (a == "b" || a == "c")
- {
- count++;
- }
- }
- sw.Stop();
- report("||, not found", count);
- sw.Reset();
- a = "b";
- sw.Start();
- count = 0;
- for (Int32 index = 0; index < ITERATIONS; index++)
- {
- if (a == "b" || a == "c")
- {
- count++;
- }
- }
- sw.Stop();
- report("||, found", count);
- sw.Reset();
- #endregion
- #region array.Contains
- a = "a";
- sw.Start();
- count = 0;
- for (Int32 index = 0; index < ITERATIONS; index++)
- {
- if (ops.Contains(a))
- {
- count++;
- }
- }
- sw.Stop();
- report("array.Contains, not found", count);
- sw.Reset();
- a = "b";
- sw.Start();
- count = 0;
- for (Int32 index = 0; index < ITERATIONS; index++)
- {
- if (ops.Contains(a))
- {
- count++;
- }
- }
- sw.Stop();
- report("array.Contains, found", count);
- sw.Reset();
- #endregion
- #region array.Contains
- a = "a";
- sw.Start();
- count = 0;
- for (Int32 index = 0; index < ITERATIONS; index++)
- {
- if (CreateArray().Contains(a))
- {
- count++;
- }
- }
- sw.Stop();
- report("array.Contains, inline array, not found", count);
- sw.Reset();
- a = "b";
- sw.Start();
- count = 0;
- for (Int32 index = 0; index < ITERATIONS; index++)
- {
- if (CreateArray().Contains(a))
- {
- count++;
- }
- }
- sw.Stop();
- report("array.Contains, inline array, found", count);
- sw.Reset();
- #endregion
- #region switch-statement
- a = GetString().Substring(0, 1); // avoid interned string
- sw.Start();
- count = 0;
- for (Int32 index = 0; index < ITERATIONS; index++)
- {
- switch (a)
- {
- case "b":
- case "c":
- count++;
- break;
- }
- }
- sw.Stop();
- report("switch-statement, not interned, not found", count);
- sw.Reset();
- a = GetString().Substring(1, 1); // avoid interned string
- sw.Start();
- count = 0;
- for (Int32 index = 0; index < ITERATIONS; index++)
- {
- switch (a)
- {
- case "b":
- case "c":
- count++;
- break;
- }
- }
- sw.Stop();
- report("switch-statement, not interned, found", count);
- sw.Reset();
- #endregion
- #region switch-statement
- a = "a";
- sw.Start();
- count = 0;
- for (Int32 index = 0; index < ITERATIONS; index++)
- {
- switch (a)
- {
- case "b":
- case "c":
- count++;
- break;
- }
- }
- sw.Stop();
- report("switch-statement, interned, not found", count);
- sw.Reset();
- a = "b";
- sw.Start();
- count = 0;
- for (Int32 index = 0; index < ITERATIONS; index++)
- {
- switch (a)
- {
- case "b":
- case "c":
- count++;
- break;
- }
- }
- sw.Stop();
- report("switch-statement, interned, found", count);
- sw.Reset();
- #endregion
- }
- }
- private static String GetString()
- {
- return "ab";
- }
- private static String[] CreateArray()
- {
- return new String[] { "b", "c" };
- }
- }
- }
- switch (a) {
- case "b":
- case "c":
- // variable a is either "b" or "c"
- break;
- }
- if ("bc".Contains(a)) { } // Maybe check a.Length == 1, too.
- if ((a[0] & 0x62) == 0x62) { } // Maybe check a.Length == 1, too.
- if (new String[] { "b", "c" }.Contains(a)) { }
- if(Regex.IsMatch(a, "b|c"))
- if(Regex.IsMatch(a, "^(b|c)$"))
- [Flags]
- enum MyEnum {
- None = 0,
- A = 1,
- B = 2,
- C = 4,
- D = 8
- }
- //...
- MyEnum a = MyEnum.B
- if((a & (MyEnum.B | MyEnum.C)) > 0)
- // do something
- if((a & MyEnum.B) > 0 || (a & MyEnum.C) > 0)
- // do something
- None = 00000
- A = 00001
- B = 00010
- C = 00100
- D = 01000
- B 00010
- & C 00100
- ---------
- 00110
- (B & C) 00110
- & (a = B) 00010
- ---------------
- 00010
- public static bool Any(object a, params object[] b)
- {
- foreach(object item in b)
- {
- if(a == b)
- {
- return true;
- }
- }
- return false;
- }
Add Comment
Please, Sign In to add comment