Click here to Skip to main content
15,881,967 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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?

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

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:

C#
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;
    }
  });
}
Posted
Updated 13-Jul-17 0:29am
v4

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.
Quote:
It cant because of sum.

If you do careful analyze of your code, you will see that sum is not a problem.
Quote:
I added the new method that I tried with parallel.for

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.
 
Share this answer
 
v4
Comments
mdaemon 13-Jul-17 4:51am    
for example, when I add the parallel instead of first for loop, everytime Y array gives different results.
Patrice T 13-Jul-17 5:07am    
Show code
mdaemon 13-Jul-17 6:28am    
I added the new method that I tried with parallel.for
mdaemon 13-Jul-17 12:25pm    
very good answer I will research
If you have a powerful processor with enough cores, you could use parallel processing, see: Task Parallel Library: 1 of n[^]
 
Share this answer
 
Comments
mdaemon 12-Jul-17 16:12pm    
It cant because of sum. I try to use parallel.for but everytime Y array is produced differently.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900