Plinq-Parallel.ForEach for 性能提升

https://msdn.microsoft.com/zh-cn/library/dd460720.aspx

 

本示例显示如何使用 Parallel.ForEach 循环对任何 System.Collections.IEnumerable 或 System.Collections.Generic.IEnumerable<T> 数据源启用数据并行。

System_CAPS_note注意

本文档使用 lambda 表达式在 PLINQ 中定义委托。  如果您不熟悉 C# 或 Visual Basic 中的 lambda 表达式,请参见 Lambda Expressions in PLINQ and TPL。  

示例

C#
VB
 
//
// IMPORTANT!!!: Add a reference to System.Drawing.dll
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Drawing;

public class Example
{
    public static void Main()
    {
        // A simple source for demonstration purposes. Modify this path as necessary.
        String[] files = System.IO.Directory.GetFiles(@"C:\Users\Public\Pictures\Sample Pictures", "*.jpg");
        String newDir = @"C:\Users\Public\Pictures\Sample Pictures\Modified";
        System.IO.Directory.CreateDirectory(newDir);

        // Method signature: Parallel.ForEach(IEnumerable<TSource> source, Action<TSource> body)
        // Be sure to add a reference to System.Drawing.dll.
        Parallel.ForEach(files, (currentFile) => 
                                {
                                    // The more computational work you do here, the greater 
                                    // the speedup compared to a sequential foreach loop.
                                    String filename = System.IO.Path.GetFileName(currentFile);
                                    var bitmap = new Bitmap(currentFile);

                                    bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                    bitmap.Save(Path.Combine(newDir, filename));

                                    // Peek behind the scenes to see how work is parallelized.
                                    // But be aware: Thread contention for the Console slows down parallel loops!!!

                                    Console.WriteLine("Processing {0} on thread {1}", filename, Thread.CurrentThread.ManagedThreadId);
                                    //close lambda expression and method invocation
                                });


        // Keep the console window open in debug mode.
        Console.WriteLine("Processing complete. Press any key to exit.");
        Console.ReadKey();
    }
}
下面设置了最大的并发线程数
private static void TParllel()
        {
            var list = new List<int>(16000);

            for (int i = 0; i < 16000; i++)
            {
                list.Add(i);
            }
            Parallel.ForEach(list, new ParallelOptions { MaxDegreeOfParallelism = 200}, (p, state) => { Invoke(p); });
        }

http://www.cnblogs.com/huangxincheng/archive/2012/04/02/2429543.html

转载于:https://www.cnblogs.com/fangyuan303687320/p/5484905.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值