Skip to content

Instantly share code, notes, and snippets.

@jonpryor
Created April 4, 2016 19:24
Show Gist options
  • Save jonpryor/02ff6fab835b42c6212ab818ce91421d to your computer and use it in GitHub Desktop.
Save jonpryor/02ff6fab835b42c6212ab818ce91421d to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using System.Reflection;
class App {
const int C = 1000000;
static void TimeReflection (PropertyInfo p, string value)
{
var sw = Stopwatch.StartNew ();
for (int i = 0; i < C; ++i) {
int n = (int) p.GetValue (value);
}
sw.Stop ();
Console.WriteLine ("# Reflection: {0}", sw.Elapsed);
}
static void TimeDelegate (PropertyInfo p, string value)
{
var d = (Func<string, int>) Delegate.CreateDelegate (
typeof (Func<string, int>),
p.GetGetMethod ());
var sw = Stopwatch.StartNew ();
for (int i = 0; i < C; ++i) {
d (value);
}
sw.Stop ();
Console.WriteLine ("# Delegate: {0}", sw.Elapsed);
}
static void Main ()
{
var p = "".GetType ().GetProperty ("Length");
TimeReflection (p, "");
TimeDelegate (p, "");
}
}
# Reflection: 00:00:00.4263301
# Delegate: 00:00:00.0028287
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment