Why parallel code is slower? [duplicate]

戏子无情 提交于 2020-01-16 15:22:20

问题


I created a simple test with big arrays, and I get a big difference between parallel for and normal for

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var rn = new Random(541);

            var tmStart = DateTime.Now;
            int[,] a = new int[2048, 2048];
            for (int i = 0; i < 2048; i++)
                for (int j = 0; j < 2048; j++)
                    a[i, j] = rn.Next();
            var tmEnd = DateTime.Now - tmStart;
            Console.WriteLine("Normal..: {0}",tmEnd.ToString());

            tmStart = DateTime.Now;
            a = new int[2048, 2048];
            Parallel.For(0, 2048, i =>
                {
                    for (int j = 0; j < 2048; j++)
                        a[i, j] = rn.Next();
                });
            tmEnd = DateTime.Now - tmStart;
            Console.WriteLine("Parallel: {0}", tmEnd.ToString());
        }
    }
}

The time for process:

Normal..: 00:00:00.1250071
Parallel: 00:00:00.3880222

Why is that difference so big?

I imagined that if you use several threads, it would be faster...


回答1:


There are a few things wrong with your benchmark:

  1. Your tasks are so small that the threading overhead is significant.
  2. You break the threading rules with the instance of Random. Who knows what impact this has?
  3. Your timing is, well, crude.

Here's a better benchmark that shows the parallel version of the code being faster:

using System;
using System.Diagnostics;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        const int N = 1024;

        static void Main(string[] args)
        {
            int[,] a = new int[N, N];

            var stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < N; i++)
                for (int k = 0; k < N; k++)
                    for (int j = 0; j < N; j++)
                        a[i, j] = i+j;
            Console.WriteLine("Normal..: {0}", stopwatch.ElapsedMilliseconds);

            stopwatch = Stopwatch.StartNew();
            Parallel.For(0, N, i =>
            {
                for (int k = 0; k < N; k++)
                    for (int j = 0; j < N; j++)
                        a[i, j] = i+j;
            });
            Console.WriteLine("Parallel: {0}", stopwatch.ElapsedMilliseconds);

            Console.ReadLine();
        }
    }
}

And here's one which uses random number generators:

using System;
using System.Diagnostics;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        const int N = 512;

        static void Main(string[] args)
        {
            int[,] a = new int[N, N];

            var stopwatch = Stopwatch.StartNew();
            var rng = new Random();
            for (int i = 0; i < N; i++)
                for (int k = 0; k < N; k++)
                    for (int j = 0; j < N; j++)
                        a[i, j] = rng.Next();
            Console.WriteLine("Normal..: {0}", stopwatch.ElapsedMilliseconds);

            stopwatch = Stopwatch.StartNew();
            Parallel.For(0, N, i =>
            {
                var rngpar = new Random();
                for (int k = 0; k < N; k++)
                    for (int j = 0; j < N; j++)
                        a[i, j] = rngpar.Next();
            });
            Console.WriteLine("Parallel: {0}", stopwatch.ElapsedMilliseconds);

            Console.ReadLine();
        }
    }
}



回答2:


When you parallelize tasks you have some "context switch" time spent from the SO to manage various tasks. If the single task isn't doing something computationally interesting you will not perceive any benefit from multithreading. You should choose some other example in which each task is eating some CPU cycles ( even a loop would do as an exercise purpose ).



来源:https://stackoverflow.com/questions/24633605/why-parallel-code-is-slower

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!