看板 C_Sharp 關於我們 聯絡資訊
小弟只針對一維的程式去修改成多執行緒 static void Main(string[] args) { ushort[] myArray1 = new ushort[2452 * 2054]; ushort[] myArray2 = new ushort[2452 * 2054]; ushort[] myArray3 = new ushort[2452 * 2054]; //視核心數量 雙核就2個AutoResetEvent AutoResetEvent[] aREs = new AutoResetEvent[2]; Random myRnd = new Random(); TimeSpan clock01 = new TimeSpan(); int cycleTimes = 10; for (int i = 0; i < cycleTimes; i++) { //AutoResetEvent初始化 for (int j = 0; j < aREs.Length; j++) { aREs[j] = new AutoResetEvent(false); } for (int j = 0; j < myArray1.Length; j++) { //NextDouble()取整數後是0 改成這樣可能比較適當 myArray1[j] = (ushort)myRnd.Next(256*256); myArray2[j] = (ushort)myRnd.Next(256*256); } System.DateTime startTime = DateTime.Now; //把原本for loop拆成多個執行緒去執行 for (int k = 0; k < aREs.Length; k++) { int y = k; ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object o) { for (int j = y; j < myArray1.Length; j += aREs.Length) { myArray3[j] = (ushort)Math.Abs(myArray1[j] - myArray2[j]); } aREs[y].Set(); })); } WaitHandle.WaitAll(aREs); clock01 = (DateTime.Now - startTime) + clock01; } Console.WriteLine((clock01.TotalMilliseconds / cycleTimes).ToString()); Console.ReadLine(); } cpu是AthlonIIx2 240超到3.5G 用build完的執行檔跑 單執行緒平均45ms 雙執行緒平均26ms 如果是四核心的i7 每秒超過30張應該是沒問題了XD -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 124.11.129.161 ※ 編輯: DearKurt 來自: 124.11.129.161 (01/17 00:56)