So I decided to test it myself and to do my test I wrote a basic console app that would keep track of how long it took to execute consecutive if / else if statements in a standard block and then in-line.
The first test case I used was a simple math equation to determine if the value was even or odd. I ran through the application 8 times first using a standard if / else statement block and recorded my results. Then I modified the code and replaced the normal statement block with an in-line statement and ran the application another 8 times.
The following are my results and the code used, all times are in milliseconds:
Standard If Block
3845.2199, 4122.2358, 4189.2396, 2666.1525, 3759.215, 4040.2311, 2184.1249, 3958.2264
static void Main(string[] args) { DateTime dtStart = DateTime.Now; for (int i = 0; i < Int16.MaxValue; i++) { int Remainder; Math.DivRem(i, 2, out Remainder); if (Remainder > 0) Console.WriteLine("Odd"); else Console.WriteLine("Even"); } TimeSpan tsDuration = (DateTime.Now - dtStart); Console.WriteLine(tsDuration.TotalMilliseconds.ToString()); Console.ReadKey(); }
In-line If Block
4437.2538, 1890.1081, 2824.1616, 3628.2075, 3169.1812, 2745.2142, 2180.1247, 2149.123
static void Main(string[] args) { DateTime dtStart = DateTime.Now; for (int i = 0; i < Int16.MaxValue; i++) { int Remainder; Math.DivRem(i, 2, out Remainder); Console.WriteLine((Remainder > 0 ? "Odd" : "Even")); } TimeSpan tsDuration = (DateTime.Now - dtStart); Console.WriteLine(tsDuration.TotalMilliseconds.ToString()); Console.ReadKey(); }
As you can see the only consistency is that they are very inconsistent. However, the standard if block averages out to be 3595.58 milliseconds. While, the in-line if statement averages out to 2877.92 making it faster.
Next I decided to try a standard if / else if / else statement to mix it up a bit. The following are my results and code, again all times are in milliseconds.
Standard If / Else If Block
2841.1625, 4112.2352, 4367.2498, 2451.1402, 3063.1752, 3928.2247, 3220.184, 4052.2318
static void Main(string[] args) { DateTime dtStart = DateTime.Now; for (int i = 0; i < Int16.MaxValue; i++) { int Remainder; Math.DivRem(i, 3, out Remainder); if (Remainder == 0) Console.WriteLine("Remainder 0"); else if (Remainder == 1) Console.WriteLine("Remainder 1"); else Console.WriteLine("Remainder 2"); } TimeSpan tsDuration = (DateTime.Now - dtStart); Console.WriteLine(tsDuration.TotalMilliseconds.ToString()); Console.ReadKey(); }
In-line If / Else If Block
2317.1325, 4169.2385, 4242.2426, 1964.1124, 4384.2507, 3329.1904, 3561.2037, 3339.191
static void Main(string[] args) { DateTime dtStart = DateTime.Now; for (int i = 0; i < Int16.MaxValue; i++) { int Remainder; Math.DivRem(i, 3, out Remainder); Console.WriteLine((Remainder == 0 ? "Remainder 0" : (Remainder == 1) ? "Remainder 1" : "Remainder 2")); } TimeSpan tsDuration = (DateTime.Now - dtStart); Console.WriteLine(tsDuration.TotalMilliseconds.ToString()); Console.ReadKey(); }
Great Research! I’ve always wondered this myself.
Good one.