MaxDegreeOfParallelism = Environment.ProcessorCount减慢执行时间我的CPU上 [英] MaxDegreeOfParallelism = Environment.ProcessorCount slows down execution time on my CPU

查看:1893
本文介绍了MaxDegreeOfParallelism = Environment.ProcessorCount减慢执行时间我的CPU上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下程序(即从我的 http://blogs.msdn.com/b/csharpfaq/archive/2010/06/01/parallel-programming-in-net-framework-4-getting- started.aspx )的分割使用的的Parallel.For 循环

I have the following program (that I got from http://blogs.msdn.com/b/csharpfaq/archive/2010/06/01/parallel-programming-in-net-framework-4-getting-started.aspx) that splits a task up using Parallel.For loop

class Program
{
    static void Main(string[] args)
    {
        var watch = Stopwatch.StartNew();


        Parallel.For(2, 20, (i) =>
        {
            var result = SumRootN(i);
            Console.WriteLine("root {0} : {1} ", i, result);
        });

        Console.WriteLine(watch.ElapsedMilliseconds);
        Console.ReadLine();
    }

    public static double SumRootN(int root)
    {
        double result = 0;
        for (int i = 1; i < 10000000; i++)
        {
            result += Math.Exp(Math.Log(i) / root);
        }
        return result;
    }
}

当我运行该测试了几次,我得到的时候:

When I run this test several times I get times of:

1992年,2140,1783,1863年毫秒等等等等

1992, 2140, 1783, 1863 ms etc etc.

我的第一个问题是,当我在下面添加为什么时代总是不同的?? 我做的每一次却把次,每次变化完全相同的计算。

My first question is, why are the times always different?? I am doing the exact same calculations each time yet the times vary each time.

现在代码可以使用所有可用处理器的我的CPU上:

Now when I add in the following code to make use of all the available processors on my CPU:

        var parallelOptions = new ParallelOptions
        {
            MaxDegreeOfParallelism = Environment.ProcessorCount    (On my CPU this is 8)
        };

        Parallel.For(2, 20, parallelOptions, (i) =>
        {
            var result = SumRootN(i);
            Console.WriteLine("root {0} : {1} ", i, result);
        });



我注意到,执行时间实际上增加了!现在的时间是:

I notice that the execution times actually increase!! The times are now:

2192,3192,2603,2245毫秒等等等等

2192, 3192, 2603, 2245 ms etc etc.

为什么。这也导致次增加?我使用这个错误?

推荐答案

从的http://msdn.microsoft.com/en-us/library/system.threading.tasks.paralleloptions .maxdegreeofparallelism(v = vs.110)的.aspx

默认情况下,的 的ForEach 将利用然而,许多线程的基本调度程序提供。从默认更改MaxDegreeOfParallelism唯一的限制多少并发任务会被使用。

By default, For and ForEach will utilize however many threads the underlying scheduler provides. Changing MaxDegreeOfParallelism from the default only limits how many concurrent tasks will be used.

这意味着,设置 MaxDegreeOfParallelism 来的处理器的数量将实际上限制的Parallel.For 循环的使用线程的最佳量的工作负荷的能力。例如,我有一个围绕600迭代的长期运行的代码使用接近60线程迁移作业,不是每个处理器的 1线程多很多的限制,你想设置的。

This means that setting MaxDegreeOfParallelism to the number of processors will in fact limit the capacity of the Parallel.For loop to use the optimal amount of threads for the work load. For example, I have a migration job that uses close to 60 threads on around 600 iterations of long-running code, a lot more than the 1 thread per processor limit that you're trying to set.

MaxDegreeOfParallelism ThreadPool.SetMaxThreads 只应若使用你明确需要防止比线程给定数目的更多的执行。例如,如果使用Access数据库,我想将它设置为64,因为这是一个可以通过访问一个单一的过程中进行处理的并发连接的最大数目。

MaxDegreeOfParallelism or ThreadPool.SetMaxThreads should only be used if you explicitly need to prevent more than a given number of threads from executing. For example, if using an Access database, I would set it to 64, because that's the maximum number of concurrent connections that can be handled by Access for a single process.

这篇关于MaxDegreeOfParallelism = Environment.ProcessorCount减慢执行时间我的CPU上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆