优化嵌套循环c#

时间:2015-03-25 17:06:57

标签: c# loops optimization nested parallel.for

我正在尝试优化以下代码的平衡 - 有多次迭代会耗费大量时间。看看Parallel.For示例,但不知道如何在我的情况下使用它们,因为我在每个“for”循环中有“双”变量。你能帮忙吗?感谢

public void maximization ()
    {
        int ii = 0; // counter to alter file name
        int jj = 0; // counter for all output strings

        string[] calculation;

        ArrayList array = new ArrayList(); // temporary array for optimized parameters

        for (pr_lower1 = -0.02; pr_lower1 <= 0.0; pr_lower1 = pr_lower1 + 0.002) //11
        {
            for (pr_upper1 = 0.002; pr_upper1 <= 0.02; pr_upper1 = pr_upper1 + 0.002) //10
            {
                for (OI_lower1 = 0.02; OI_lower1 <= 0.1; OI_lower1 = OI_lower1 + 0.01) //9
                {
                    for (pr_lower2 = -0.02; pr_lower2 <= 0.0; pr_lower2 = pr_lower2 + 0.002) //11
                    {
                        for (pr_upper2 = 0.002; pr_upper2 <= 0.02; pr_upper2 = pr_upper2 + 0.002) //10
                        {
                            for (OI_lower2 = -0.1; OI_lower2 <= 0.0; OI_lower2 = OI_lower2 + 0.01) //11
                            {
                                for (OI_upper2 = 0.01; OI_upper2 <= 0.1; OI_upper2 = OI_upper2 + 0.01) //10
                                {
                                    for (stop = 0.05; stop <= 0.27; stop = stop + 0.02) //12
                                    {
                                        for (tp = 0.05; tp <= 0.27; tp = tp + 0.02) //12
                                        {                                                
                                            this.run_algo_max();
                                            jj++;
                                            string output = ret + " " + jj;
                                            array.Add(output);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            ii++;
            calculation = (String[])array.ToArray(typeof(string));
            array.Clear();
            File.WriteAllLines(Application.StartupPath + "\\results" + ii + ".txt", calculation);
        }
    }

1 个答案:

答案 0 :(得分:1)

为了并行化,您必须重写run_algo_max方法,以便将值作为参数并返回结果。

在这种情况下,您可以发送两个参数并计算相应的值。 (156816000是除第一个之外的所有循环长度的乘积。)您可以将结果直接写入文件,这样您就不需要在内存中保留数百万个字符串:

for (int ii = 0; ii < 11; ii++) {
  int jj = ii * 156816000;
  File.WriteAllLines(
    Application.StartupPath + "\\results" + ii + ".txt",

    Enumerable.Range(0, 156816000)
    .AsParallel()
    .Select(n => this.run_algo_max(ii, n) + " " + (jj + n))
  );
}

该方法将获取参数,计算值,完成工作并返回结果:

public string run_algo_max(int ii, int n) {
  double pr_lower1 = -0.02 + ii * 0.002;
  double pr_upper1 = 0.002 + (n % 10) * 0.002; n /= 10;
  double OI_lower1 = 0.02 + (n % 9) * 0.01; n /= 9;
  // etc.
  double stop = 0.05 + (n % 12) * 0.02; n /= 12;
  double tp = 0.05 + n * 0.02;
  // do the work, produce a string "result"
  return result;
}