Вопрос по performance, c# – Неутешительная производительность с Parallel.For

16

Error: User Rate Limit ExceededParallel.ForError: User Rate Limit ExceededforError: User Rate Limit ExceededParallel.ForError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

var loops = 200;
var perloop = 10000000;

var sum = 0.0;
for (var k = 0; k < loops; ++k)
{
    var sumk = 0.0;
    for (var i = 0; i < perloop; ++i) sumk += (1.0 / i) * i;
    sum += sumk;
}

Error: User Rate Limit Exceeded

sum = 0.0;
Parallel.For(0, loops,
                k =>
                    {
                        var sumk = 0.0;
                        for (var i = 0; i < perloop; ++i) sumk += (1.0 / i) * i;
                        sum += sumk;
                    });

Error: User Rate Limit ExceededsumError: User Rate Limit Exceeded

In Release build ("optimize code" flag set) the sequential for loop takes 33.7 s on my computer, whereas the Parallel.For loop takes 8.4 s, a performance ratio of only 4.0.

Error: User Rate Limit Exceeded

ParallelOptions.MaxDegreesOfParallelism = Environment.ProcessorCount

Error: User Rate Limit ExceededparallelError: User Rate Limit Exceeded

Sequential vs. parallel CPU utilization

Error: User Rate Limit ExceededдоError: User Rate Limit Exceeded

UPDATE

Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededdisabledError: User Rate Limit ExceededfasterError: User Rate Limit ExceededforError: User Rate Limit ExceededforError: User Rate Limit ExceededParallel.ForError: User Rate Limit Exceeded

Error: User Rate Limit ExceededforError: User Rate Limit ExceededenabledError: User Rate Limit ExceededParallel.ForError: User Rate Limit ExceededenabledError: User Rate Limit ExceededParallel.ForError: User Rate Limit Exceeded2.5Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededforError: User Rate Limit Exceeded

Error: User Rate Limit Exceededsuperuser.com/questions/279629/… Jacob Sobus
Error: User Rate Limit Exceeded Anders Gustafsson
Error: User Rate Limit Exceeded Steve B
Error: User Rate Limit Exceeded Magnus
Error: User Rate Limit ExceededthisError: User Rate Limit Exceeded Eugene Ryabtsev

Ваш Ответ

4   ответа
1

    for (int i = 0; i < 10; i++)
    {
        int[] array = new int[] { 0, 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 };
        Stopwatch watch = new Stopwatch();
        watch.Start();
        //Parallel foreach
        Parallel.ForEach(array, line =>
        {
            for (int x = 0; x < 1000000; x++)
            {

            }

        });

        watch.Stop();
        Console.WriteLine("Parallel.ForEach {0}", watch.Elapsed.Milliseconds);
        watch = new Stopwatch();
        //foreach
        watch.Start();
        foreach (int item in array)
        {
            for (int z = 0; z < 10000000; z++)
            {

            }
        }
        watch.Stop();
        Console.WriteLine("ForEach {0}", watch.Elapsed.Milliseconds);

        Console.WriteLine("####");
    }
    Console.ReadKey();

enter image description here

Error: User Rate Limit Exceeded
23

        int[] nums = Enumerable.Range(0, 1000000).ToArray();
        long total = 0;

        // Use type parameter to make subtotal a long, not an int
        Parallel.For<long>(0, nums.Length, () => 0, (j, loop, subtotal) =>
        {
            subtotal += nums[j];
            return subtotal;
        },
            (x) => Interlocked.Add(ref total, x)
        );

subtotaltotal

Error: User Rate Limit Exceeded Anders Gustafsson
Error: User Rate Limit Exceeded Anders Gustafsson
5

int[] nums = Enumerable.Range(0, 1000000).ToArray();
long total = 0;

Parallel.ForEach(
    Partitioner.Create(0, nums.Length),
    () => 0,
    (part, loopState, partSum) =>
    {
        for (int i = part.Item1; i < part.Item2; i++)
        {
            partSum += nums[i];
        }
        return partSum;
    },
    (partSum) =>
    {
        Interlocked.Add(ref total, partSum);
    }
);

Error: User Rate Limit ExceededdynamicError: User Rate Limit ExceededafterError: User Rate Limit Exceeded
Error: User Rate Limit Exceededdocs.microsoft.com/en-us/dotnet/standard/parallel-programming/…Error: User Rate Limit ExceededParallel.ForEachError: User Rate Limit ExceededautomaticallyError: User Rate Limit Exceededdoes notError: User Rate Limit ExceededPartitioner.CreateError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
6

Error: User Rate Limit Exceeded Anders Gustafsson
Error: User Rate Limit Exceededen.wikipedia.org/wiki/Gustafson's_lawError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded Anders Gustafsson

Похожие вопросы