Skip to content

Instantly share code, notes, and snippets.

@iwantedue
Created October 25, 2013 07:28
Show Gist options
  • Save iwantedue/7150746 to your computer and use it in GitHub Desktop.
Save iwantedue/7150746 to your computer and use it in GitHub Desktop.
Test performance of overload vs optional in simple use case
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