欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

DotNet中怎么實現(xiàn)并行計算

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)DotNet中怎么實現(xiàn)并行計算,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

專注于為中小企業(yè)提供成都網(wǎng)站設(shè)計、成都做網(wǎng)站服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)烏魯木齊免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。

誤區(qū)三 . 并行計算是運行時的事

的確,DotNet會在運行時決定是否使用并行庫處理代碼,但是早在你編譯代碼時,編譯器就早已為這一時刻做好準(zhǔn)備,換就話說:

1. 使用并行庫處理代碼與普通方式對比,IL的結(jié)構(gòu)是不同的。

2. 即使你選擇使用并行計算,并且你也確實擁有多核??(線程)CPU,運行時你的代碼也不一定是并行的。

使用TPL后CLR可能會分解任務(wù),這一依據(jù)的其中之一是由IL支持的,IL將并行的任務(wù)代碼分離,以便在將來的操作中并行,這一點可以從以下的示例中看出來,以下兩段示例的核心C#代碼都是Tostring()和Sleep(),Code A使用For包含Sleep,Code B使用Parallel.For處理:

Code Part A:

IL:

IL_000e: callvirt instance void [System]System.Diagnostics.Stopwatch::Start()   IL_0013: nop   IL_0014: ldc.i4.0   IL_0015: stloc.2   IL_0016: br.s IL_0031   IL_0018: nop   IL_0019: ldloca.s i   IL_001b: call instance string [mscorlib]System.Int32::ToString()   IL_0020: stloc.0   IL_0021: ldc.i4 0xc8   IL_0026: call void [mscorlib]System.Threading.Thread::Sleep(int32)   IL_002b: nop   IL_002c: nop   IL_002d: ldloc.2   IL_002e: ldc.i4.1   IL_002f: add   IL_0030: stloc.2   IL_0031: ldloc.2   IL_0032: ldc.i4.s 10   IL_0034: clt   IL_0036: stloc.3   IL_0037: ldloc.3   IL_0038: brtrue.s IL_0018   IL_003a: ldloc.1   IL_003b: callvirt instance void [System]System.Diagnostics.Stopwatch::Stop()

我們注意到,Code Part A的Sleep是直接出現(xiàn)在Load方法中的。

DotNet中怎么實現(xiàn)并行計算

再來看看Parallel方式:

Code Part B:

Form1_Load:

IL_0019: callvirt instance void [System]System.Diagnostics.Stopwatch::Start()   IL_001e: nop   IL_001f: ldc.i4.0   IL_0020: ldc.i4.s 10   IL_0022: ldloc.1   IL_0023: ldftn instance void WindowsFormsApplication4.Form1/'<>c__DisplayClass1'::'<Form1_Load>b__0'(int32)   IL_0029: newobj instance void class [mscorlib]System.Action`1<int32>::.ctor(object, native int) IL_002e: call valuetype [mscorlib]System.Threading.Tasks.ParallelLoopResult [mscorlib]System.Threading.Tasks.Parallel::For(int32, int32, class [mscorlib]System.Action`1<int32>)   IL_0033: pop   IL_0034: ldloc.0   IL_0035: callvirt instance void [System]System.Diagnostics.Stopwatch::Stop()   //注意,Sleep已經(jīng)不在Load方法中了,而是被一個“b__0”代替,并行代碼與宿主代碼分離,以下就是b__0的  IL: .method public hidebysig instance void '<Form1_Load>b__0'(int32 i) cil managed   {   // 代碼大小 26 (0x1a)   .maxstack 8   IL_0000: nop   IL_0001: ldarg.0   IL_0002: ldarga.s i   IL_0004: call instance string [mscorlib]System.Int32::ToString()   IL_0009: stfld string WindowsFormsApplication4.Form1/'<>c__DisplayClass1'::a IL_000e: ldc.i4 0xc8   IL_0013: call void [mscorlib]System.Threading.Thread::Sleep(int32)   IL_0018: nop   IL_0019: ret   } // end of method '<>c__DisplayClass1'::'<Form1_Load>b__0'

結(jié)構(gòu)圖:

DotNet中怎么實現(xiàn)并行計算

以上的紅色代碼就是在Code A中出現(xiàn)的主要代碼。再讓我們重溫一下這張圖,IL的代碼任務(wù)已經(jīng)很明顯的指示了出來。

DotNet中怎么實現(xiàn)并行計算

每當(dāng)我們增加一個并行代碼段,IL中就會增加一個b_N塊:假如我們的代碼中包含兩個Parallel塊,每塊的主代碼與上述一致,IL如下:

IL_0019: callvirt instance void [System]System.Diagnostics.Stopwatch::Start()   IL_001e: nop   IL_001f: ldc.i4.0   IL_0020: ldc.i4.s 10   IL_0022: ldloc.1 IL_0023: ldftn instance void WindowsFormsApplication4.Form1/'<>c__DisplayClass2'::'<Form1_Load>b__0'(int32)   IL_0029: newobj instance void class [mscorlib]System.Action`1<int32>::.ctor(object, native int) IL_002e: call valuetype [mscorlib]System.Threading.Tasks.ParallelLoopResult [mscorlib]System.Threading.Tasks.Parallel::For(int32, int32, class [mscorlib]System.Action`1<int32>)   IL_0033: pop  IL_0034: ldc.i4.0   IL_0035: ldc.i4.s 10   IL_0037: ldloc.1   IL_0038: ldftn instance void WindowsFormsApplication4.Form1/'<>c__DisplayClass2'::'<Form1_Load>b__1'(int32)   IL_003e: newobj instance void class [mscorlib]System.Action`1<int32>::.ctor(object, native int) IL_0043: call valuetype [mscorlib]System.Threading.Tasks.ParallelLoopResult [mscorlib]System.Threading.Tasks.Parallel::For(int32, int32, class [mscorlib]System.Action`1<int32>)   IL_0048: pop   IL_0049: ldloc.0   IL_004a: callvirt instance void [System]System.Diagnostics.Stopwatch::Stop()

下圖中會有對應(yīng)模塊出現(xiàn):

DotNet中怎么實現(xiàn)并行計算

上述就是小編為大家分享的DotNet中怎么實現(xiàn)并行計算了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

本文名稱:DotNet中怎么實現(xiàn)并行計算
轉(zhuǎn)載來于:http://chinadenli.net/article20/geoijo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄網(wǎng)站建設(shè)網(wǎng)站改版搜索引擎優(yōu)化建站公司虛擬主機

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管