如何顺序循环的运行速度比在C#中的并行循环? [英] How does sequential loop run faster than Parallel loop in C#?

查看:102
本文介绍了如何顺序循环的运行速度比在C#中的并行循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过一个很小的例子:

I tried a very minimal example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Collections.Concurrent;
using System.Diagnostics;

namespace TPLExample {
    class Program {
        static void Main(string[] args) {
            int[] dataItems = new int[100];
            double[] resultItems = new double[100];

            for (int i = 0; i < dataItems.Length; ++i) {
                dataItems[i] = i;
            }

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Reset();
            stopwatch.Start();
            Parallel.For(0, dataItems.Length, (index) => {
                resultItems[index] = Math.Pow(dataItems[index], 2);
            });
            stopwatch.Stop();
            Console.WriteLine("TPL Time elapsed: {0}", stopwatch.Elapsed);

            stopwatch.Reset();
            stopwatch.Start();
            for (int i = 0; i < dataItems.Length; ++i) {
                resultItems[i] = Math.Pow(dataItems[i], 2);
            }
            stopwatch.Stop();
            Console.WriteLine("Sequential Time elapsed: {0}", stopwatch.Elapsed);

            WaitForEnterKey();
        }

        public static void WaitForEnterKey() {
            Console.WriteLine("Press enter to finish");
            Console.ReadLine();
        }

        public static void PrintMessage() {
            Console.WriteLine("Message printed");
        }
    }
}

输出是:

TPL Time elapsed: 00:00:00.0010670
Sequential Time elapsed: 00:00:00.0000178
Press enter to finish

的顺序循环比第三方物流的方式更快!这怎么可能?从我的理解,在计算的Parallel.For 将并行执行,因此,它必须更快?

The sequential loop is way faster than TPL! How is this possible? From my understanding, calculation within the Parallel.For will be executed in parallel, so must it be faster?

推荐答案

简而言之:对于只迭代百余项,并进行一个小的数学运算,产生新的线程,等着他们去完成产生比仅仅通过将循环运行更多的开销。

Simply put: For only iterating over a hundred items and performing a small mathematical operation, spawning new threads and waiting for them to complete produces more overhead than just running through the loop would.

从我的理解,在内部的Parallel.For计算将并行执行,因此,它必须更快?

From my understanding, calculation within the Parallel.For will be executed in parallel, so must it be faster?

随着人们做出有关计算机性能一览无余陈述一般情况,还有在作怪远远更多的变数在这里,你真的不能作出这样的假设。例如,你的循环内部,你正在做的比 Math.Pow 仅此而已,其处理器可以很好执行很快。如果这是一个I / O密集型操作,需要每个线程等待很长的时间,甚至如果它是一个系列处理器密集型操作的,你会获得更多的并行处理(假设你有一个多线程处理器) 。但是,因为它是创建和同步这些线程的开销是远远超过任何好处大于并行性可能会给你。

As generally happens when people make sweeping statements about computer performance, there are far more variables at play here, and you can't really make that assumption. For example, inside your for loop, you are doing nothing more than Math.Pow, which the processor can perform very quickly. If this were an I/O intensive operation, requiring each thread to wait a long time, or even if it were a series of processor-intensive operations, you would get more out of Parallel processing (assuming you have a multi-threaded processor). But as it is, the overhead of creating and synchronizing these threads is far greater than any advantage that parallelism might give you.

这篇关于如何顺序循环的运行速度比在C#中的并行循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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