Skip to content

Instantly share code, notes, and snippets.

@analogrelay
Last active December 13, 2015 23:29
Show Gist options
  • Save analogrelay/4992468 to your computer and use it in GitHub Desktop.
Save analogrelay/4992468 to your computer and use it in GitHub Desktop.
Curious about Lambdas vs Method Groups...
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Curious
{
class Program
{
private static readonly long Iterations = 100000000;
static void Main(string[] args)
{
// Warm up the methods
Dump("warmup");
Call(_ => { });
Console.WriteLine("Press Enter to start");
Console.ReadLine();
Console.WriteLine("Running Method Group Test");
Stopwatch outer = new Stopwatch();
outer.Start();
for (int i = 0; i < Iterations; i++)
{
Call(Dump);
}
outer.Stop();
double elapsedNs = ((double)outer.ElapsedTicks/(double)Stopwatch.Frequency)*1000*1000;
Console.WriteLine("Finished Method Group Test");
Console.WriteLine("Elapsed: " + elapsedNs.ToString("E4") + "ns");
Console.WriteLine("Average: " + ((double)elapsedNs / (double)Iterations).ToString("E4") + "ns");
Console.WriteLine("High-Precision? " + (Stopwatch.IsHighResolution ? "Yes" : "No"));
Console.WriteLine("Press Enter to Continue");
Console.ReadLine();
Console.WriteLine("Running Lambda Test");
outer.Reset();
outer.Start();
for (int i = 0; i < Iterations; i++)
{
Call(s => Dump(s));
}
outer.Stop();
elapsedNs = ((double)outer.ElapsedTicks / (double)Stopwatch.Frequency) * 1000 * 1000;
Console.WriteLine("Finished Lambda Test");
Console.WriteLine("Elapsed: " + elapsedNs.ToString("E4") + "ns");
Console.WriteLine("Average: " + ((double)elapsedNs / (double)Iterations).ToString("E4") + "ns");
Console.WriteLine("High-Precision? " + (Stopwatch.IsHighResolution ? "Yes" : "No"));
Console.WriteLine("Press Enter to End");
Console.ReadLine();
}
private static void Call(Action<string> act)
{
act("Hi!");
}
private static void Dump(string str)
{
}
private void MethodGroup()
{
Call(Dump);
}
private void Lambda()
{
Call(s => Dump(s));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment