Skip to content

Instantly share code, notes, and snippets.

@gyuwon
Last active February 16, 2016 02:53
Show Gist options
  • Save gyuwon/59ba8eb3763f559efb38 to your computer and use it in GitHub Desktop.
Save gyuwon/59ba8eb3763f559efb38 to your computer and use it in GitHub Desktop.
Compilier optimization
using System;
using System.Diagnostics;
namespace Optimization
{
public class Program
{
public static void Main(string[] args)
{
var instance = new Program();
int loop = 1000 * 1000 * 1000;
instance.Idle(loop);
instance.Direct(loop);
instance.Delegate(loop);
}
public void TargetMethod()
{
}
private void Idle(int loop)
{
Stopwatch stopwatch = Stopwatch.StartNew();
for (int i = 0; i < loop; i++)
{
}
stopwatch.Stop();
Console.WriteLine(stopwatch.Elapsed);
}
private void Direct(int loop)
{
Stopwatch stopwatch = Stopwatch.StartNew();
for (int i = 0; i < loop; i++)
{
TargetMethod();
}
stopwatch.Stop();
Console.WriteLine(stopwatch.Elapsed);
}
private void Delegate(int loop)
{
Action del = TargetMethod;
Stopwatch stopwatch = Stopwatch.StartNew();
for (int i = 0; i < loop; i++)
{
del();
}
stopwatch.Stop();
Console.WriteLine(stopwatch.Elapsed);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment