本文介绍了为什么比PLINQ LINQ慢此code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是一个双核处理器,主频为2.66GHz的机器上运行此。我不知道如果我在正确的位置的.AsParallel()调用。我直接试了一下在范围变量太多,而且还在缓慢。我不明白为什么...

下面是我的结果:

工艺非平行1000了146毫秒

并行处理了1000毫秒156

工艺非平行5000了5187毫秒

并行处理了5000 5300毫秒

 使用系统;
使用System.Collections.Generic;
使用System.Diagnostics程序;
使用System.Linq的;

命名空间DemoConsoleApp
{
  内部类节目
  {
    私有静态无效的主要()
    {
      ReportOnTimedProcess(
        ()=> GetIntegerCombinations(),
        非平行1000);

      ReportOnTimedProcess(
        ()=> GetIntegerCombinations(runAsParallel:真)
        平行1000);

      ReportOnTimedProcess(
        ()=> GetIntegerCombinations(5000),
        非平行5000);

      ReportOnTimedProcess(
        ()=> GetIntegerCombinations(5000,真),
        并行5000);

      Console.Read();
    }

    私有静态列表与所述元组LT; INT,INT>> GetIntegerCombinations(
      INT iterationCount = 1000,布尔runAsParallel = FALSE)
    {
      IEnumerable的< INT>范围= Enumerable.Range(1,iterationCount);

      IEnumerable的<元组LT; INT,INT>> integerCombinations =
        从x范围内
        从y中的范围
        选择新的元组LT; INT,INT>(X,Y);

      返回runAsParallel
               ? integerCombinations.AsParallel()了ToList()
               :integerCombinations.ToList();
    }

    私有静态无效ReportOnTimedProcess(
      行动过程中,串processName)
    {
      VAR秒表=新的秒表();
      stopwatch.Start();
      处理();
      stopwatch.Stop();

      Console.WriteLine(过程{0}把{1}毫秒,
                        processName,stopwatch.ElapsedMilliseconds);
    }
  }
}
 

解决方案

这是稍微慢一点,因为PLINQ具有一定的开销(线程,调度等),所以你要仔细挑选你会并行。这种特殊的code你基准测试是不是真的值得并行,你必须在并行与显著负荷的任务,否则开销将体重超过并行化的好处。

First off, I am running this on a dual core 2.66Ghz processor machine. I am not sure if I have the .AsParallel() call in the correct spot. I tried it directly on the range variable too and that was still slower. I don't understand why...

Here are my results:

Process non-parallel 1000 took 146 milliseconds

Process parallel 1000 took 156 milliseconds

Process non-parallel 5000 took 5187 milliseconds

Process parallel 5000 took 5300 milliseconds

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace DemoConsoleApp
{
  internal class Program
  {
    private static void Main()
    {
      ReportOnTimedProcess(
        () => GetIntegerCombinations(),
        "non-parallel 1000");

      ReportOnTimedProcess(
        () => GetIntegerCombinations(runAsParallel: true),
        "parallel 1000");

      ReportOnTimedProcess(
        () => GetIntegerCombinations(5000),
        "non-parallel 5000");

      ReportOnTimedProcess(
        () => GetIntegerCombinations(5000, true),
        "parallel 5000");

      Console.Read();
    }

    private static List<Tuple<int, int>> GetIntegerCombinations(
      int iterationCount = 1000, bool runAsParallel = false)
    {
      IEnumerable<int> range = Enumerable.Range(1, iterationCount);

      IEnumerable<Tuple<int, int>> integerCombinations =
        from x in range
        from y in range
        select new Tuple<int, int>(x, y);

      return runAsParallel
               ? integerCombinations.AsParallel().ToList()
               : integerCombinations.ToList();
    }

    private static void ReportOnTimedProcess(
      Action process, string processName)
    {
      var stopwatch = new Stopwatch();
      stopwatch.Start();
      process();
      stopwatch.Stop();

      Console.WriteLine("Process {0} took {1} milliseconds",
                        processName, stopwatch.ElapsedMilliseconds);
    }
  }
}
解决方案

It's slightly slower because PLINQ has a certain overhead (threads, scheduling, etc) so you have to pick carefully what you will parallelize. This particular code you're benchmarking isn't really worth parallelizing, you have to parallelize over tasks with significant load, otherwise the overhead will weight more than the benefits of parallelization.

这篇关于为什么比PLINQ LINQ慢此code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 04:49