Skip to content

Instantly share code, notes, and snippets.

Created September 18, 2014 19:53
Show Gist options
  • Save anonymous/31b752d24212ad43836e to your computer and use it in GitHub Desktop.
Save anonymous/31b752d24212ad43836e to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using ProductiveRage.UpdateWith; // This is from the NuGet package CSharpImmutableUpdateWith
namespace SimplePerformanceTests
{
class Program
{
static void Main(string[] args)
{
UpdateApproachOptions updateApproach;
int loopCount;
if ((args == null)
|| (args.Length != 2)
|| !Enum.TryParse<UpdateApproachOptions>(args[0], out updateApproach)
|| !int.TryParse(args[1], out loopCount)
|| (loopCount <= 0))
{
Console.WriteLine(
"{0}The update approach must be specified (one of {1}) and a positive loop count - eg.{0}{0} {2} StaticWith 10000000",
Environment.NewLine,
string.Join(", ", Enum.GetNames(typeof(UpdateApproachOptions))),
System.IO.Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
);
return;
}
var r0 = new RoleDetails("test", new DateTime(2014, 9, 18), new DateTime(2014, 10, 20));
var newTitle = "test-new";
var newEndDateIfAny = new DateTime(2014, 9, 21);
var timer = new Stopwatch();
timer.Start();
switch (updateApproach)
{
default:
throw new ArgumentException("Unsupported update approach: " + updateApproach);
case UpdateApproachOptions.ExtensionSimpleWith:
for (var i = 0; i < loopCount; i++)
r0.ExtensionSimpleWith(title: newTitle, endDateIfAny: newEndDateIfAny);
timer.Stop();
break;
case UpdateApproachOptions.FixedArgumentsStaticWith:
for (var i = 0; i < loopCount; i++)
r0.FixedArgumentsStaticWith(title: newTitle, endDateIfAny: newEndDateIfAny);
timer.Stop();
break;
case UpdateApproachOptions.ManualWith:
for (var i = 0; i < loopCount; i++)
r0.ManualWith(title: newTitle, endDateIfAny: newEndDateIfAny);
timer.Stop();
break;
case UpdateApproachOptions.SimpleWith:
for (var i = 0; i < loopCount; i++)
r0.SimpleWith(title: newTitle, endDateIfAny: newEndDateIfAny);
timer.Stop();
break;
case UpdateApproachOptions.StaticWith:
for (var i = 0; i < loopCount; i++)
r0.StaticWith(title: newTitle, endDateIfAny: newEndDateIfAny);
timer.Stop();
break;
}
Console.WriteLine(
"Total time for {0}: {1}ms for {2} loops => {3:0.0} ticks/update",
updateApproach,
timer.ElapsedMilliseconds,
loopCount,
((float)timer.ElapsedTicks) / loopCount
);
}
private enum UpdateApproachOptions
{
ExtensionSimpleWith,
FixedArgumentsStaticWith,
ManualWith,
SimpleWith,
StaticWith
}
}
public class RoleDetails
{
public RoleDetails(string title, DateTime startDate, DateTime? endDateIfAny)
{
if (string.IsNullOrWhiteSpace(title))
throw new ArgumentException("title");
if ((endDateIfAny != null) && (endDateIfAny <= startDate))
throw new ArgumentException("title");
Title = title.Trim();
StartDate = startDate;
EndDateIfAny = endDateIfAny;
}
/// <summary>
/// This will never be null or blank, it will not have any leading or trailing whitespace
/// </summary>
public string Title { get; private set; }
public DateTime StartDate { get; private set; }
/// <summary>
/// If non-null, this will greater than the StartDate
/// </summary>
public DateTime? EndDateIfAny { get; private set; }
public RoleDetails ManualWith(
Optional<string> title = new Optional<string>(),
Optional<DateTime> startDate = new Optional<DateTime>(),
Optional<DateTime?> endDateIfAny = new Optional<DateTime?>())
{
if (!title.IndicatesChangeFromValue(Title)
&& !startDate.IndicatesChangeFromValue(StartDate)
&& !endDateIfAny.IndicatesChangeFromValue(EndDateIfAny))
return this;
return new RoleDetails(title.GetValue(Title), startDate.GetValue(StartDate), endDateIfAny.GetValue(EndDateIfAny));
}
public RoleDetails SimpleWith(
Optional<string> title = new Optional<string>(),
Optional<DateTime> startDate = new Optional<DateTime>(),
Optional<DateTime?> endDateIfAny = new Optional<DateTime?>())
{
return DefaultUpdateWithHelper.GetGenerator<RoleDetails>()(this, title, startDate, endDateIfAny);
}
private static UpdateWithSignature<RoleDetails> updaterStatic
= DefaultUpdateWithHelper.GetGenerator<RoleDetails>(typeof(RoleDetails).GetMethod("StaticWith"));
public RoleDetails StaticWith(
Optional<string> title = new Optional<string>(),
Optional<DateTime> startDate = new Optional<DateTime>(),
Optional<DateTime?> endDateIfAny = new Optional<DateTime?>())
{
return updaterStatic(this, title, startDate, endDateIfAny);
}
private static UpdateWithSignature3<RoleDetails> updaterStaticFixedArguments
= DefaultUpdateWithHelper.GetUncachedGenerator3<RoleDetails>(typeof(RoleDetails).GetMethod("FixedArgumentsStaticWith"));
public RoleDetails FixedArgumentsStaticWith(
Optional<string> title = new Optional<string>(),
Optional<DateTime> startDate = new Optional<DateTime>(),
Optional<DateTime?> endDateIfAny = new Optional<DateTime?>())
{
return updaterStaticFixedArguments(this, title, startDate, endDateIfAny);
}
}
public static class RoleDetailsExtensions
{
public static RoleDetails ExtensionSimpleWith(
this RoleDetails source,
Optional<string> title = new Optional<string>(),
Optional<DateTime> startDate = new Optional<DateTime>(),
Optional<DateTime?> endDateIfAny = new Optional<DateTime?>())
{
if (source == null)
throw new ArgumentNullException("source");
return DefaultUpdateWithHelper.GetGenerator<RoleDetails>()(source, title, startDate, endDateIfAny);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment