.NET(C#) Internals: 以一個數組填充的例子初步瞭解.NET 4.0中的並行(一)

引言

隨着CPU多核的普及,編程時充分利用這個特性越顯重要。本文首先用傳統的嵌套循環進行數組填充,然後用.NET 4.0中的System.Threading.Tasks提供的Parallel Class來並行地進行填充(當然這裏也用到嵌套循環),通過對比發現其中差異。主要內容如下:

  1. 通常的數組填充
  2. 並行的組數填充
  3. 性能比較
  4. System.Threading.Tasks分析,這個將在續篇.NET(C#) Internals: 以一個數組填充的例子初步瞭解.NET 4.0中的並行(二)中介紹

1、通常的數組填充

首先看如下代碼:

  1. using System;  
  2.  
  3. namespace ParallelForSample  
  4. {  
  5.   public class SingleCore  
  6.   {  
  7.     public static void Calculate(int calcVal)  
  8.     {  
  9.       Utility util = new Utility();  
  10.       util.Start();  
  11.  
  12.       int[,] G = new int[calcVal, calcVal];  
  13.       for (int k = 0; k < calcVal; k++)  
  14.         for (int i = 0; i < calcVal; i++)  
  15.           for (int j = 0; j < calcVal; j++)  
  16.             G[i, j] = Math.Min(G[i, j], G[i, k] + G[k, j]);  
  17.       util.Stop();  
  18.  
  19.     }  
  20.   }  

上面的粗體紅色顯示的幾行代碼就是實現數組填充,這個很好理解不用多費口舌。補充說明的是:上面的Utility是爲了統計性能而編寫的一個類,它主要就是用到了Stopwatch對象——它提供一組方法和屬性,可用於準確地測量運行時間。Utility的代碼如下:

  1. public class Utility  
  2.     {  
  3.         private Stopwatch _stopwatch;  
  4.         public void Start()  
  5.         {  
  6.             _stopwatch = new Stopwatch();  
  7.             _stopwatch.Start();  
  8.         }  
  9.  
  10.         public void Stop()  
  11.         {  
  12.             _stopwatch.Stop();  
  13.             TimeSpan ts = _stopwatch.Elapsed;  
  14.             string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",  
  15.                 ts.Hours, ts.Minutes, ts.Seconds,  
  16.                 ts.Milliseconds / 10);  
  17.             Console.WriteLine("Time taken : {0}", elapsedTime);  
  18.         }  
  19.     } 

利用它我們就可以對數組填充所耗費的時間進行計算了。

2、並行的組數填充

爲了充分利用CPU的多核,我們編寫如下代碼:

  1. using System;  
  2. using System.Threading.Tasks;  
  3.  
  4. namespace ParallelForSample  
  5. {  
  6.   public class MultiCore  
  7.   {  
  8.     public static void Calculate(int calcVal)  
  9.     {  
  10.       Utility util = new Utility();  
  11.       util.Start();  
  12.  
  13.       int[,] G = new int[calcVal, calcVal];  
  14.  
  15.       Parallel.For(0, calcVal,  
  16.         delegate(int k)  
  17.         {  
  18.           Parallel.For(0, calcVal, delegate(int i)  
  19.           {  
  20.             for (int j = 0; j < calcVal; j++)  
  21.               G[i, j] = Math.Min(G[i, j], G[i, k] + G[k, j]);  
  22.           });  
  23.         }  
  24.       );  
  25.  
  26.       util.Stop();  
  27.     }  
  28.   }  

留意上面的紅色粗體顯示的幾行代碼,它利用了Parallel.For Method (Int32, Int32, Action<Int32>)方法,Parallel類位於命名空間System.Threading.Tasks中,它支持並行循環。此Parallel.For方法使得它裏面的迭代可能並行地運行,注意到上述代碼中它的第三個參數是一個委託。在(0,calcVal)之間,這個委託將被調用。

3、性能比較

現在我們來測試一下,上面兩種方法的執行性能差異如何,下載源碼。其實,核心代碼已經在上面貼出來了,現在注意是編寫實例來測試,代碼主要如下:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.  
  6. namespace ParallelForSample  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             Console.WriteLine("Single core");  
  13.             SingleCore.Calculate(1000);  
  14.             Console.WriteLine("Multi core");  
  15.             MultiCore.Calculate(1000);  
  16.             Console.WriteLine("Finished");  
  17.             Console.ReadKey();  
  18.         }  
  19.     }  

運行之後得到如下結果:(不同電腦配置不同,得出結果不同)

以一個數組填充的例子初步瞭解.NET 4.0中的並行(一)

圖1、性能比較

從結果可以看出,並行的數組填充比通常的數組填充性能更高。

  1. System.Threading.Tasks分析,這個將在續篇.NET(C#) Internals: 以一個數組填充的例子初步瞭解.NET 4.0中的並行(二)中介紹……
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章