Skip to content

Instantly share code, notes, and snippets.

@PhilipRieck
Created October 6, 2010 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PhilipRieck/613265 to your computer and use it in GitHub Desktop.
Save PhilipRieck/613265 to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
namespace QuickCS
{
interface ITarget
{
void DoNothing(int i);
}
class Target : ITarget
{
public Target(Tester eventRaiser)
{
eventRaiser.AnEvent += DoNothing;
}
private int _i;
public void DoNothing(int i)
{
_i = i;
}
}
class Tester
{
private const int IterationCount = 10000000;
public event Action<int> AnEvent;
public void Go()
{
var t = new Target(this);
var sw = new Stopwatch();
sw.Reset();
sw.Start();
for (var i = 0; i < IterationCount; i++)
{
t.DoNothing(i);
}
sw.Stop();
Console.WriteLine("{1} direct reference calls : \t{0} s", sw.ElapsedMilliseconds / 1000.0, IterationCount);
ITarget ifTarget = t;
sw.Reset();
sw.Start();
for (var i = 0; i < IterationCount; i++)
{
ifTarget.DoNothing(i);
}
sw.Stop();
Console.WriteLine("{1} calls through an interface : \t{0} s", sw.ElapsedMilliseconds / 1000.0, IterationCount);
sw.Reset();
sw.Start();
for (var i = 0; i < IterationCount; i++)
{
AnEvent(i);
}
sw.Stop();
Console.WriteLine("{1} invocations of an event : \t{0} s", sw.ElapsedMilliseconds / 1000.0, IterationCount);
var anAction = new Action<int>(t.DoNothing);
sw.Reset();
sw.Start();
for (var i = 0; i < IterationCount; i++)
{
anAction(i);
}
sw.Stop();
Console.WriteLine("{1} calls through Action<int> : \t{0} s", sw.ElapsedMilliseconds / 1000.0, IterationCount);
Console.ReadLine();
}
}
class Program
{
public static void Main()
{
new Tester().Go();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment