1. public class Person
  2. {
  3. public string Name { get; set; }
  4. public Person(string name)
  5. {
  6. this.Name = name;
  7. }
  8. public string Say(string msg)
  9. {
  10. return $"{Name}: {msg}";
  11. }
  12. }
  13. class Program
  14. {
  15. // 测试次数
  16. const int count = 10000000;
  17. static void Main(string[] args)
  18. {
  19. CreateInstance0();
  20. CreateInstance1();
  21. CreateInstance2();
  22. CreateInstance3();
  23. CreateInstance4();
  24. Console.Read();
  25. }
  26. static void CreateInstance0()
  27. {
  28. Stopwatch watch = new Stopwatch();
  29. watch.Start();
  30. for (var i = 0; i < count; i++)
  31. {
  32. Person person = new Person("张三");
  33. }
  34. watch.Stop();
  35. Console.WriteLine($"{watch.Elapsed} - new");
  36. }
  37. static void CreateInstance1()
  38. {
  39. Stopwatch watch = new Stopwatch();
  40. watch.Start();
  41. for (var i = 0; i < count; i++)
  42. {
  43. object person = Activator.CreateInstance(typeof(Person), "张三");
  44. }
  45. watch.Stop();
  46. Console.WriteLine($"{watch.Elapsed} - Activator.CreateInstance");
  47. }
  48. static void CreateInstance2()
  49. {
  50. Assembly assembly = Assembly.GetExecutingAssembly();
  51. Stopwatch watch = new Stopwatch();
  52. watch.Start();
  53. for (var i = 0; i < count; i++)
  54. {
  55. Person obj = (Person)assembly.CreateInstance("ConsoleTest.Person", true, BindingFlags.Default, null, new object[] { "张三" }, null, null);
  56. }
  57. watch.Stop();
  58. Console.WriteLine($"{watch.Elapsed} - Assembly.CreateInstance");
  59. }
  60. static void CreateInstance3()
  61. {
  62. Assembly assembly = Assembly.GetExecutingAssembly();
  63. Stopwatch watch = new Stopwatch();
  64. watch.Start();
  65. for (var i = 0; i < count; i++)
  66. {
  67. Type type = assembly.GetType("ConsoleTest.Person");
  68. object person = Activator.CreateInstance(type, "张三");
  69. }
  70. watch.Stop();
  71. Console.WriteLine($"{watch.Elapsed} - Assembly.CreateInstance1");
  72. }
  73. static void CreateInstance4()
  74. {
  75. Assembly assembly = Assembly.GetExecutingAssembly();
  76. Stopwatch watch = new Stopwatch();
  77. watch.Start();
  78. Type type = assembly.GetType("ConsoleTest.Person");
  79. for (var i = 0; i < count; i++)
  80. {
  81. object person = Activator.CreateInstance(type, "张三");
  82. }
  83. watch.Stop();
  84. Console.WriteLine($"{watch.Elapsed} - Assembly.CreateInstance2");
  85. }
  86. }

  • 通过反射实例化对象,要比直接 new 要慢 50 倍左右
  • assembly.CreateInstance 要比 Activator.CreateInstance 慢,主要的性能损耗在 Assembly.GetType
  1. class Program
  2. {
  3. // 测试次数
  4. const int count = 10000000;
  5. static void Main(string[] args)
  6. {
  7. InvokeMethod0();
  8. InvokeMethod1();
  9. InvokeMethod2();
  10. InvokeMethod3();
  11. InvokeMethod4();
  12. Console.Read();
  13. }
  14. static void InvokeMethod0()
  15. {
  16. Person person = new Person("张三");
  17. Stopwatch watch = new Stopwatch();
  18. watch.Start();
  19. for (var i = 0; i < count; i++)
  20. {
  21. string name = person.Say("Hello World!");
  22. }
  23. watch.Stop();
  24. Console.WriteLine($"{watch.Elapsed} - 直接调用");
  25. }
  26. static void InvokeMethod1()
  27. {
  28. Person person = (Person)Activator.CreateInstance(typeof(Person), "张三");
  29. Stopwatch watch = new Stopwatch();
  30. watch.Start();
  31. for (var i = 0; i < count; i++)
  32. {
  33. string name = person.Say("Hello World!");
  34. }
  35. watch.Stop();
  36. Console.WriteLine($"{watch.Elapsed} - 反射缓存类调用");
  37. }
  38. static void InvokeMethod2()
  39. {
  40. Person person = (Person)Activator.CreateInstance(typeof(Person), "张三");
  41. MethodInfo method = typeof(Person).GetMethod(nameof(Person.Say), new Type[] { typeof(string) });
  42. Func<string, string> func = (Func<string, string>)method.CreateDelegate(typeof(Func<string, string>), person);
  43. Stopwatch watch = new Stopwatch();
  44. watch.Start();
  45. for (var i = 0; i < count; i++)
  46. {
  47. string result = func("Hello World!");
  48. }
  49. watch.Stop();
  50. Console.WriteLine($"{watch.Elapsed} - 使用反射创建出来的委托调用");
  51. }
  52. static void InvokeMethod3()
  53. {
  54. Person person = (Person)Activator.CreateInstance(typeof(Person), "张三");
  55. MethodInfo method = typeof(Person).GetMethod(nameof(Person.Say), new Type[] { typeof(string) });
  56. object[] parameters = new object[] { "Hello World!" };
  57. Stopwatch watch = new Stopwatch();
  58. watch.Start();
  59. for (var i = 0; i < count; i++)
  60. {
  61. string name = (string)method.Invoke(person, parameters);
  62. }
  63. watch.Stop();
  64. Console.WriteLine($"{watch.Elapsed} - 使用反射得到的方法缓存调用");
  65. }
  66. static void InvokeMethod4()
  67. {
  68. Person person = (Person)Activator.CreateInstance(typeof(Person), "张三");
  69. object[] parameters = new object[] { "Hello World!" };
  70. Stopwatch watch = new Stopwatch();
  71. watch.Start();
  72. for (var i = 0; i < count; i++)
  73. {
  74. string result = (string)(typeof(Person).GetMethod(nameof(Person.Say))?.Invoke(person, parameters));
  75. }
  76. watch.Stop();
  77. Console.WriteLine($"{watch.Elapsed} - 直接使用反射调用");
  78. }
  79. }

  • 反射得到实例后调用方法和直接调用方法效率一样
  • 缓存反射方法调用和直接使用反射调用都非常耗效率

版权声明:本文为gl1573原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/gl1573/p/14411427.html