Created
April 4, 2016 19:24
-
-
Save jonpryor/02ff6fab835b42c6212ab818ce91421d to your computer and use it in GitHub Desktop.
This file contains hidden or 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; | |
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, ""); | |
} | |
} |
This file contains hidden or 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
# 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