Skip to content

Instantly share code, notes, and snippets.

@KumoKairo
Created February 1, 2017 05:15
Show Gist options
  • Save KumoKairo/0ac544ade19f56dbc4c107d03919bb69 to your computer and use it in GitHub Desktop.
Save KumoKairo/0ac544ade19f56dbc4c107d03919bb69 to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
namespace NamedArgsSpeedTest
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 100; i++)
{
ConductPerfTest();
}
Console.ReadKey();
}
private static void ConductPerfTest()
{
var testObject = new TestClass(3, 5, 6);
var sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 1000000; i++)
{
testObject.SetSomeValuesClassicaly(5, 3, 6);
}
sw.Stop();
Console.WriteLine("Classic " + sw.ElapsedMilliseconds);
sw.Reset();
sw.Start();
for (int i = 0; i < 1000000; i++)
{
testObject.SetSomeValuesNamed(x: 51);
}
sw.Stop();
Console.WriteLine("Named " + sw.ElapsedMilliseconds);
Console.WriteLine();
}
}
}
namespace NamedArgsSpeedTest
{
public class TestClass
{
public int x;
public int y;
public int z;
public TestClass()
{
}
public TestClass(int x, int y, int z)
{
this.x = x;
this.y = y;
this.z = z;
}
public void SetSomeValuesClassicaly(int x, int y, int z)
{
this.x = x;
this.y = y;
this.z = z;
}
public void SetSomeValuesNamed(int? x = null, int? y = null, int? z = null)
{
this.x = x.GetValueOrDefault(this.x);
this.y = y.GetValueOrDefault(this.y);
this.z = z.GetValueOrDefault(this.z);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment