Created
October 6, 2010 12:28
-
-
Save PhilipRieck/613265 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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