如何提高嵌套for循环的性能 [英] How to improve the performance of nested for loop

查看:149
本文介绍了如何提高嵌套for循环的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码运行正常。但是,嵌套的for循环运行3.5秒,我必须运行此方法50次。所以这需要太多时间。我该如何优化?



The code runs true. However the nested for loop runs 3.5 second and I have to run this method 50 times. So it takes too much time. How can I optimize?

object.Y is jagged array [1406][21]
object.dif is jagged array [1406][1405]
object.E = 21



在填充了双重和NAN的Y锯齿状数据之后,我对每一行进行排序,然后找到非NAN元素的索引。



我尝试了什么:




After Y jagged array filled up with double and NAN, I sort every row then I find index of non NAN elements.

What I have tried:

private void Calculate(Obj object)
{
  double sum = 0;
  int i = 1406;
  int j = 1405;
  for (int t = 0; t < i; t++)
  {
    object.dif[t] = new double[j];
    for (int l = 0; l < j; l++)
    {
      if (Math.Abs(t - l) > object.E)
      {
        for (int k = 0; k < object.E; k++)
        {
          sum += (object.Y[t][k] - object.Y[l][k]) * (object.Y[t][k] - object.Y[l][k]);
        }
        object.dif[t][l] = Math.Sqrt(sum);
      }
      else
        object.dif[t][l] = double.NaN;
    sum= 0;
    }
  }
}

//With Parallel For but because of Sum, everytime Y matrix give different results
private void Calculate(Obj object)
{
  double sum = 0;
  int i = 1406;
  int j = 1405;
   Parallel.For(0,i,t=>{
    object.dif[t] = new double[j];
    for (int l = 0; l < j; l++)
    {
      if (Math.Abs(t - l) > object.E)
      {
        for (int k = 0; k < object.E; k++)
        {
          sum += (object.Y[t][k] - object.Y[l][k]) * (object.Y[t][k] - object.Y[l][k]);
        }
        object.dif[t][l] = Math.Sqrt(sum);
      }
      else
        object.dif[t][l] = double.NaN;
    sum= 0;
    }
  });
}

推荐答案

此代码非常简单,在不知道其使用情况下无法删除任何内容。

为减少运行时间,唯一的可能是使用并行处理。

This code is pretty much minimum and nothing can be removed without knowing its use.
To reduce runtime, the only possibility is using parallel processing.
引用:

因为总和不能。



如果你仔细分析你的代码,你会发现总和不是问题。


If you do careful analyze of your code, you will see that sum is not a problem.

Quote:

我添加了我尝试使用parallel.for的新方法

I added the new method that I tried with parallel.for



And它不起作用。

你需要分析代码的作用!

你需要代码的哪一部分总和

你在哪里计算总和?你在哪里使用总和

你在哪里初始化总和?

会发生什么?和并行化循环时?

使用调试器查看代码正在做什么。

-----

有一个工具可以让你看到你的代码在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]

Visual Basic / Visual Studio视频教程 - 基本调试 - YouTube [ ^ ]

初学者的Visual Basic .NET编程 - 断点和调试工具 [ ^ ]



在Visual Studio中调试C#代码 - YouTube [ ^ ]



调试器在这里显示你的内容代码正在做,你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期效果时,你就接近了一个错误。


And it don't work.
You need to analyze what the code do !
In which part of the code do you need sum ?
Where do you calculate sum ? Where do you use sum ?
Where do you initialize sum ?
What happen to sum when you parallelize the loop ?
Use the debugger to see what your code is doing.
-----
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
Visual Basic / Visual Studio Video Tutorial - Basic Debugging - YouTube[^]
Visual Basic .NET programming for Beginners - Breakpoints and Debugging Tools[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


如果你有一个功能强大的处理器和足够的内核,你可以使用并行处理,请参阅:任务并行库:n中的1个 [ ^ ]
If you have a powerful processor with enough cores, you could use parallel processing, see: Task Parallel Library: 1 of n[^]

这篇关于如何提高嵌套for循环的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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