Created
October 25, 2013 07:28
-
-
Save iwantedue/7150746 to your computer and use it in GitHub Desktop.
Test performance of overload vs optional in simple use case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
namespace OverloadOrOptional | |
{ | |
public class Program | |
{ | |
public static void OverloadParamMethod(bool input) | |
{ | |
OverloadParamMethod(input, null); | |
} | |
private static int OverloadParamMethodCounter = 0; | |
public static void OverloadParamMethod(bool input, string input2) | |
{ | |
OverloadParamMethodCounter++; | |
} | |
private static int OptionalParamMethodCounter = 0; | |
public static void OptionalParamMethod(bool input, string input2 = null) | |
{ | |
OptionalParamMethodCounter++; | |
} | |
public static void Main(string[] args) | |
{ | |
OptionalParamMethod(true); | |
OptionalParamMethod(false, "hello"); | |
OverloadParamMethod(true); | |
OverloadParamMethod(false, "hello"); | |
var sw = Stopwatch.StartNew(); | |
for (int i = 0; i < 100000000; i++) | |
{ | |
OverloadParamMethod(true); | |
OverloadParamMethod(false, "hello"); | |
} | |
sw.Stop(); | |
Console.WriteLine("Overload {0} ticks", sw.ElapsedTicks); | |
var sw2 = Stopwatch.StartNew(); | |
for (int i = 0; i < 100000000; i++) | |
{ | |
OptionalParamMethod(true); | |
OptionalParamMethod(false, "hello"); | |
} | |
sw2.Stop(); | |
Console.WriteLine("Optional {0} ticks", sw2.ElapsedTicks); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment