1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
 
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> source = new List<int>();
            for (int i = 0; i < 1000; i++)
            {
                source.Add(i);
            }
            Stopwatch stop = new Stopwatch();
            stop.Start();
            var result = (from x in source.AsParallel().WithDegreeOfParallelism(50)
                         select proc(x)).ToList();
            Console.WriteLine(stop.Elapsed);
 
            stop.Stop();
            Console.Read();
 
        }
        public static int proc(int x) {
            Thread.Sleep(100);
            return 1;
        }
    }
}

  耗时12秒 不使用并行 耗时100秒

posted on   xuelei被占用了  阅读(1175)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示