Skip to content

Instantly share code, notes, and snippets.

@anurse anurse/Program.cs
Last active Dec 13, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.