Skip to content

Instantly share code, notes, and snippets.

@kendallmiller
Created December 22, 2015 00:14
Show Gist options
  • Select an option

  • Save kendallmiller/3bb762cd4d609d979aed to your computer and use it in GitHub Desktop.

Select an option

Save kendallmiller/3bb762cd4d609d979aed to your computer and use it in GitHub Desktop.
namespace Delegate_Perf_Test
{
[TestFixture]
public class Delegate_Performance_Comparision
{
[Test]
public void MeasureDelegate()
{
int n = 50000000;
Action<int> d = MethodNoInlining;
var stopwatch = Stopwatch.StartNew();
for (int i = 0; i < n; ++i)
{
d(i);
}
stopwatch.Stop();
Console.WriteLine("Delegate Call: " + stopwatch.Elapsed);
stopwatch = Stopwatch.StartNew();
for (int i = 0; i < n; ++i)
{
MethodNoInlining(i);
}
stopwatch.Stop();
Console.WriteLine("Direct Call Not Inlined: " + stopwatch.Elapsed);
stopwatch = Stopwatch.StartNew();
for (int i = 0; i < n; ++i)
{
MethodWithInlining(i);
}
stopwatch.Stop();
Console.WriteLine("Direct Call Inlined: " + stopwatch.Elapsed);
}
[MethodImpl(MethodImplOptions.NoInlining)]
private void MethodNoInlining(int x)
{
x = x + 1;
}
private void MethodWithInlining(int x)
{
x = x + 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment