自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

分析與對比CLR Via C#靜態(tài)構造函數(shù)的性能

開發(fā) 后端
CLR Via C#可以選擇調(diào)用構造函數(shù)的次數(shù)從而來生成執(zhí)行更快的代碼,本文就寫一段測試代碼來看看究竟怎樣。

本文主要對CLR Via C# 靜態(tài)構造函數(shù)的性能進行分析與對比,筆者用簡單的語言描述了CLR Via C#靜態(tài)構造函數(shù),希望能給你帶來幫助。

1 CLR Via C#靜態(tài)構造函數(shù)是私有的(private) ,而且不能人為去修改訪問修飾符。

2 CLR Via C#靜態(tài)構造函數(shù)不應該去調(diào)用基類的靜態(tài)構造函數(shù),因為靜態(tài)字段不會被繼承到子類。

3 CLR Via C#靜態(tài)構造函數(shù)在一個類型中有且僅有一個,并且是無參的。

4 CLR Via C#靜態(tài)構造函數(shù)中只能初始化靜態(tài)字段。

從上面的***點可以知道靜態(tài)構造函數(shù)都是private的,所以不能顯示區(qū)進行調(diào)用,關于JIT何時會去生成調(diào)用靜態(tài)構造函數(shù)的代碼。存在著兩種說法。通常被稱為Precise和BeforeFieldInit。

l Precise方式JIT編譯器生成調(diào)用的時機:***創(chuàng)建類型的代碼之前;訪問類的非繼承字段或成員代碼之前。

l BeforeFieldInit方式JIT編譯器生成調(diào)用的時機:在訪問費繼承靜態(tài)字段代碼之前。

這兩種方式的主要區(qū)別就是選擇調(diào)用靜態(tài)構造函數(shù)的時機是否是確定的,Precise方式CLR會在確定的時刻調(diào)用靜態(tài)構造函數(shù),而BeforeFieldInit方式CLR可以自由選擇調(diào)用靜態(tài)構造函數(shù)的時機,利用這一點,CLR可以根據(jù)類型是否在程序域中加載來選擇靜態(tài)構造函數(shù)的調(diào)用次數(shù),以便能生成執(zhí)行更快的代碼。

下面來看個類分別用CLR Via C#展現(xiàn)了這兩種方式

  1. public class UserPrecise  
  2. {  
  3.  public static string _name = "內(nèi)聯(lián)賦值:oec2003";  
  4. static UserPrecise()  
  5.  {  
  6.  _name = "構造函數(shù)賦值:oec2003";  
  7.  }  
  8. }  
  9. public class UserBeforeFieldInit  
  10. {  
  11.  public static string _name = "內(nèi)聯(lián)賦值:oec2003";  
  12. }  
  13.  

通過IL代碼可以看出在UserBeforeFieldInit 的元數(shù)據(jù)上有BeforeFieldInit的標記,如下圖:

CLR Via C# 靜態(tài)構造函數(shù)性能的分析與測試
CLR Via C# 靜態(tài)構造函數(shù)性能的分析與測試

既然上面提到BeforeFieldInit方式CLR Via C#可以選擇調(diào)用構造函數(shù)的次數(shù)從而來生成執(zhí)行更快的代碼,下面就寫一段測試代碼來看看究竟怎樣。

  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# 靜態(tài)構造函數(shù)性能的分析與測試
CLR Via C# 靜態(tài)構造函數(shù)性能的分析與測試

從上面結果來看,BeforeFieldInit方式的執(zhí)行速度還是要快很多,但為什么第二次執(zhí)行時,兩種方式的速度差不多呢?因為經(jīng)過***次執(zhí)行后JIT編譯器知道類型的構造器已經(jīng)被調(diào)用了,所以第二次執(zhí)行時不會顯示對構造函數(shù)進行調(diào)用。

以上就是對CLR Via C# 靜態(tài)構造函數(shù)性能的分析與測試。

【編輯推薦】

  1. 淺談CLR線程池的缺點及解決方法
  2. CLR線程池的作用與原理淺析
  3. 簡單介紹CLR泛型及其優(yōu)勢
  4. 淺談CLR 4.0安全模型的運作機制
  5. 微軟MVP教你如何看懂.NET CLR基本術語
責任編輯:阡陌 來源: 博客園
相關推薦

2009-10-23 11:31:05

CLR Via C#調(diào)

2009-09-18 09:02:45

CLR Via C#

2024-12-31 00:07:12

2009-10-19 14:25:16

靜態(tài)構造函數(shù)

2009-07-31 15:37:45

C#靜態(tài)構造函數(shù)

2009-08-20 14:28:00

C#靜態(tài)構造函數(shù)

2009-07-31 15:44:02

C#靜態(tài)構造函數(shù)

2009-08-13 18:02:11

C#靜態(tài)構造函數(shù)

2009-11-17 09:07:55

靜態(tài)構造函數(shù)

2011-06-14 12:27:38

C#C++

2011-06-11 21:36:44

C#C++

2009-09-18 10:18:30

CLR Via

2009-10-22 19:11:25

CLR Via C#教

2009-08-13 17:30:30

C#構造函數(shù)

2009-08-13 18:10:31

C#靜態(tài)構造函數(shù)

2009-07-31 14:15:38

C# 構造函數(shù)

2009-08-24 18:09:13

C#構造函數(shù)

2009-07-30 15:24:13

C#析構函數(shù)C#構造函數(shù)

2009-08-13 18:26:35

C#繼承構造函數(shù)

2009-10-22 18:41:49

CLR VIA C#教
點贊
收藏

51CTO技術棧公眾號