1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using System.Diagnostics;
6: using System.Reflection;
7:
8: namespace ConsoleApplication
9: {
10: public class Echoer
11: {
12: public string Echo(string message)
13: {
14: return message;
15: }
16: }
17:
18: class Program
19: {
20: static void Main(string[] args)
21: {
22: Console.WriteLine("10 Static Invocations: {0} ms", staticTest(10));
23: Console.WriteLine("10 Dynamic Invocations: {0} ms", dynamicTest(10));
24: Console.WriteLine("10 Reflected Invocations: {0} ms\n", reflectedTest(10));
25:
26: Console.WriteLine("100 Static Invocations: {0} ms", staticTest(100));
27: Console.WriteLine("100 Dynamic Invocations: {0} ms", dynamicTest(100));
28: Console.WriteLine("100 Reflected Invocations: {0} ms\n", reflectedTest(100));
29:
30: Console.WriteLine("1,000 Static Invocations: {0} ms", staticTest(1000));
31: Console.WriteLine("1,000 Dynamic Invocations: {0} ms", dynamicTest(1000));
32: Console.WriteLine("1,000 Reflected Invocations: {0} ms\n", reflectedTest(1000));
33:
34: Console.WriteLine("10,000 Static Invocations: {0} ms", staticTest(10000));
35: Console.WriteLine("10,000 Dynamic Invocations: {0} ms", dynamicTest(10000));
36: Console.WriteLine("10,000 Reflected Invocations: {0} ms\n", reflectedTest(10000));
37:
38: Console.WriteLine("100,000 Static Invocations: {0} ms", staticTest(100000));
39: Console.WriteLine("100,000 Dynamic Invocations: {0} ms", dynamicTest(100000));
40: Console.WriteLine("100,000 Reflected Invocations: {0} ms\n", reflectedTest(100000));
41:
42: Console.WriteLine("1,000,000 Static Invocations: {0} ms", staticTest(1000000));
43: Console.WriteLine("1,000,000 Dynamic Invocations: {0} ms", dynamicTest(1000000));
44: Console.WriteLine("1,000,000 Reflected Invocations: {0} ms\n", reflectedTest(1000000));
45:
46: Console.ReadKey();
47: }
48:
49: static long staticTest(int times)
50: {
51: Echoer staticEchoer = new Echoer();
52: Stopwatch stopwatch = new Stopwatch();
53: stopwatch.Start();
54:
55: for (int i = 0; i < times; i++)
56: {
57: staticEchoer.Echo("hello world");
58: }
59:
60: stopwatch.Stop();
61: return stopwatch.ElapsedMilliseconds;
62: }
63:
64: static long dynamicTest(int times)
65: {
66: dynamic dynamicEchoer = new Echoer();
67: Stopwatch stopwatch = new Stopwatch();
68: stopwatch.Start();
69:
70: for (int i = 0; i < times; i++)
71: {
72: dynamicEchoer.Echo("hello world");
73: }
74:
75: stopwatch.Stop();
76: return stopwatch.ElapsedMilliseconds;
77: }
78:
79: static long reflectedTest(int times)
80: {
81: object reflectedEchoer = new Echoer();
82: Type reflectedEchoerType = reflectedEchoer.GetType();
83: Stopwatch stopwatch = new Stopwatch();
84: stopwatch.Start();
85:
86: for (int i = 0; i < times; i++)
87: {
88: reflectedEchoerType.InvokeMember("Echo", BindingFlags.InvokeMethod, null, reflectedEchoer, new object[] { "hello world" });
89: }
90:
91: stopwatch.Stop();
92: return stopwatch.ElapsedMilliseconds;
93: }
94: }
95: }
96: