分析与对比CLR Via C#静态构造函数的性能

开发 后端
CLR Via C#可以选择调用构造函数的次数从而来生成执行更快的代码,本文就写一段测试代码来看看究竟怎样。

本文主要对CLR Via C# 静态构造函数的性能进行分析与对比,笔者用简单的语言描述了CLR Via C#静态构造函数,希望能给你带来帮助。

1 CLR Via C#静态构造函数是私有的(private) ,而且不能人为去修改访问修饰符。

2 CLR Via C#静态构造函数不应该去调用基类的静态构造函数,因为静态字段不会被继承到子类。

3 CLR Via C#静态构造函数在一个类型中有且仅有一个,并且是无参的。

4 CLR Via C#静态构造函数中只能初始化静态字段。

从上面的***点可以知道静态构造函数都是private的,所以不能显示区进行调用,关于JIT何时会去生成调用静态构造函数的代码。存在着两种说法。通常被称为Precise和BeforeFieldInit。

l Precise方式JIT编译器生成调用的时机:***创建类型的代码之前;访问类的非继承字段或成员代码之前。

l BeforeFieldInit方式JIT编译器生成调用的时机:在访问费继承静态字段代码之前。

这两种方式的主要区别就是选择调用静态构造函数的时机是否是确定的,Precise方式CLR会在确定的时刻调用静态构造函数,而BeforeFieldInit方式CLR可以自由选择调用静态构造函数的时机,利用这一点,CLR可以根据类型是否在程序域中加载来选择静态构造函数的调用次数,以便能生成执行更快的代码。

下面来看个类分别用CLR Via C#展现了这两种方式

  1. public class UserPrecise  
  2. {  
  3.  public static string _name = "内联赋值:oec2003";  
  4. static UserPrecise()  
  5.  {  
  6.  _name = "构造函数赋值:oec2003";  
  7.  }  
  8. }  
  9. public class UserBeforeFieldInit  
  10. {  
  11.  public static string _name = "内联赋值:oec2003";  
  12. }  
  13.  

通过IL代码可以看出在UserBeforeFieldInit 的元数据上有BeforeFieldInit的标记,如下图:

CLR Via C# 静态构造函数性能的分析与测试
CLR Via C# 静态构造函数性能的分析与测试

既然上面提到BeforeFieldInit方式CLR Via C#可以选择调用构造函数的次数从而来生成执行更快的代码,下面就写一段测试代码来看看究竟怎样。

  1. public sealed class Program  
  2. {  
  3.  static void Main(string[] args)  
  4. {  
  5.  const Int32 iterations = 1000 * 1000 * 1000;  
  6.  Test1(iterations);  
  7.  Test2(iterations);  
  8.  }  
  9. private static void Test1(Int32 iterations)  
  10. {  
  11.  Stopwatch sw = Stopwatch.StartNew();  
  12.  for (Int32 i = 0; i < iterations; i++)  
  13.  {  
  14. UserBeforeFieldInit._name = "oec2003";  
  15. }  
  16.  Console.WriteLine("Test1-UserBeforeFieldInit 用时:" + sw.Elapsed);  
  17.  sw = Stopwatch.StartNew();  
  18.  for (Int32 j = 0; j < iterations; j++)  
  19. {  
  20. UserPrecise._name = "oec2003";  
  21. }  
  22.  Console.WriteLine("Test1-UserPrecise 用时:" + sw.Elapsed);  
  23.  }  
  24.  private static void Test2(Int32 iterations)  
  25. {  
  26. Stopwatch sw = Stopwatch.StartNew();  
  27. for (Int32 i = 0; i < iterations; i++)  
  28. {  
  29.  UserBeforeFieldInit._name = "oec2003";  
  30. }  
  31. Console.WriteLine("Test2-UserBeforeFieldInit 用时:" + sw.Elapsed);  
  32.  sw = Stopwatch.StartNew();  
  33.  for (Int32 j = 0; j < iterations; j++)  
  34.  {  
  35. UserPrecise._name = "oec2003";  
  36.  }  
  37. Console.WriteLine("Test2-UserPrecise 用时:" + sw.Elapsed);  
  38.  }  
  39. }  
  40.  public class UserBeforeFieldInit  
  41. {  
  42. public static string _name;  
  43.  }  
  44.  public class UserPrecise  
  45.  {  
  46.  public static string _name ;  
  47.  static UserPrecise()  
  48.  {  
  49. _name = "oec2003";  
  50. }  
  51.  } 

CLR Via C#测试结果如下:

CLR Via C# 静态构造函数性能的分析与测试
CLR Via C# 静态构造函数性能的分析与测试

从上面结果来看,BeforeFieldInit方式的执行速度还是要快很多,但为什么第二次执行时,两种方式的速度差不多呢?因为经过***次执行后JIT编译器知道类型的构造器已经被调用了,所以第二次执行时不会显示对构造函数进行调用。

以上就是对CLR Via C# 静态构造函数性能的分析与测试。

【编辑推荐】

  1. 浅谈CLR线程池的缺点及解决方法
  2. CLR线程池的作用与原理浅析
  3. 简单介绍CLR泛型及其优势
  4. 浅谈CLR 4.0安全模型的运作机制
  5. 微软MVP教你如何看懂.NET CLR基本术语
责任编辑:阡陌 来源: 博客园
相关推荐

2009-10-23 11:31:05

CLR Via C#调

2009-09-18 09:02:45

CLR Via C#

2009-10-19 14:25:16

静态构造函数

2009-07-31 15:37:45

C#静态构造函数

2009-08-20 14:28:00

C#静态构造函数

2009-07-31 15:44:02

C#静态构造函数

2009-08-13 18:02:11

C#静态构造函数

2011-06-11 21:36:44

C#C++

2011-06-14 12:27:38

C#C++

2009-11-17 09:07:55

静态构造函数

2009-09-18 10:18:30

CLR Via

2009-10-22 19:11:25

CLR Via C#教

2009-08-13 17:30:30

C#构造函数

2009-08-13 18:10:31

C#静态构造函数

2009-08-24 18:09:13

C#构造函数

2009-07-31 14:15:38

C# 构造函数

2009-07-30 15:24:13

C#析构函数C#构造函数

2009-08-13 18:26:35

C#继承构造函数

2009-10-22 18:41:49

CLR VIA C#教

2009-08-13 17:38:42

C#构造函数
点赞
收藏

51CTO技术栈公众号