Parallel.ForEach() 並行迴圈

來源:互聯網
上載者:User

現在的電腦幾乎都是多核的,但在軟體中並還沒有跟上這個節奏,大多數軟體還是採用傳統的方式,並沒有很好的發揮多核的優勢。
微軟的並行運算平台(Microsoft’s Parallel Computing Platform (PCP))提供了這樣一個工具,讓軟體開發人員可以有效使用多核提供的效能。
Parallel.ForEach()和Parallel.For()就是微軟並發類的成員。
今天做了一個簡單的測試,我的電腦是雙核的,效果還是比較明顯的。
一般的for和foreach迴圈用時都在10秒鐘;並發for迴圈在0.5秒,並發foreach在0.1秒鐘。

但是並發迴圈不能濫用,在簡單的少次數迴圈下,並發迴圈可能會體現不出其優勢。
下面是簡單的測試代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace parallelForeach
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime startTime;
            TimeSpan resultTime;
            List<entityA> source = new List<entityA>();
            for (int i = 0; i < 100; i++)
            {
                source.Add(new entityA
                {
                    name = "悟空" + i,
                    sex = i % 2 == 0 ? "男" : "女",
                    age = i
                });
            }
            startTime = System.DateTime.Now;
            loop1(source);
            resultTime = System.DateTime.Now - startTime;
            Console.WriteLine("一般for迴圈耗時:" + resultTime);
            startTime = System.DateTime.Now;
            loop2(source);
            resultTime = System.DateTime.Now - startTime;
            Console.WriteLine("一般foreach迴圈耗時:" + resultTime);
            startTime = System.DateTime.Now;
            loop3(source);
            resultTime = System.DateTime.Now - startTime;
            Console.WriteLine("並行for迴圈耗時:" + resultTime.Milliseconds);
            startTime = System.DateTime.Now;
            loop4(source);
            resultTime = System.DateTime.Now - startTime;
            Console.WriteLine("並行foreach迴圈耗時:" + resultTime.Milliseconds);
            Console.ReadLine();
        }

//普通的for迴圈
        static void loop1(List<entityA> source)
        {
            int count = source.Count();
            for (int i = 0; i < count; i++)
            {
                System.Threading.Thread.Sleep(100);
            }
        }

//普通的foreach迴圈
        static void loop2(List<entityA> source)
        {
            foreach (entityA item in source)
            {
                System.Threading.Thread.Sleep(100);
            }
        }

//並行的for迴圈
        static void loop3(List<entityA> source)
        {
            int count = source.Count();
            Parallel.For(0, count, item =>
            {
                System.Threading.Thread.Sleep(100);
            });
        }

//並行的foreach迴圈
        static void loop4(List<entityA> source)
        {
            Parallel.ForEach(source, item =>
            {
                System.Threading.Thread.Sleep(100);
            });
        }
    }

//簡單的實體
    class entityA
    {
        public string name { set; get; }
        public string sex { set; get; }
        public int age { set; get; }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.