0

I am working on optimising my code and found out that the problem was my variables. As I like to have an organised and readable code, different variables have different classes and I just call them to the Main method and calculations are done there.

In this way each of my outputs took 12 mins. Local variables were defined and time needed for output reach to 6 mins which is a very achievement. What is my problem:

I don't want to change all my code, so after calculation, results stored in local variables were copied to the original variables. This process (copying results) take 20% of time, how can I improve it?

Static void Main(String[] args)
{
    Vector3D temp=new Vector3D();
    double[,] Xvelocity= new double[nx,ny];
    double[,] Yvelocity= new double[nx,ny];
    //
    // some calculations on local variables
    //
    // saving results back in the original variables
    for (int i = 1; i <nx; i++)
    {
        for (int j = 1; j <ny; j++)
        {
            // Local variables: Xvelocity[i,j], Yvelocity[i,j], Temp[i,j]  
            temp.X=Xvelocity[i,j];
            temp.Y=Yvelocity[i,j];
            theSpace.TheCells[i, j, 0].Velocity=temp;
            theSpace.TheCells[i, j, 0].Tempreture=Temp[i,j];
        }
    }
}

If I do not want to copy results to original arrays, how can I have access to local variables which are defined in Main method from other classes?

Mehdi
  • 211
  • 2
  • 11
  • 7
    This question appears to be off-topic because it belongs on [Code Review](http://codereview.stackexchange.com/) – DGibbs Jul 17 '14 at 08:20
  • 2
    If you don't want to copy, don't. Pass the arrays as parameters. Since they are already references, no copy will be made. – David Heffernan Jul 17 '14 at 08:21
  • 1
    You can't. Local variables defined in methods are only visible to that method. Would it not be better to refactor your code so that the calculations are done in a separate common/shared method/class which returns the result, then you can call it from the main method. – HaukurHaf Jul 17 '14 at 08:21
  • 1
    Looks like there is no way to significantly optimize copying itself. Try building solution in Release mode. As for accessing variables - pass them as parameters, or define as properties of some class and assign to an object of that class. Or (in worst case) define them as static properties of the Main class. – Sasha Jul 17 '14 at 08:21
  • Please show the original working code (`Statice` is not a C# keyword), showing the "slow code" would be very helpful as well. – Richard Jul 17 '14 at 08:22
  • @HaukurHaf "You can't". I don't understand your statement. It is clear you should be able to pass a reference type method variable to another method. I like do it...ALL THE TIME. In fact its hard not to when you write functional C#. – Aron Jul 17 '14 at 08:32
  • @Aron, yes of course you can do that. I assumed that the OP wanted to access the variables as if they were defined in a global scope (with out passing them around as references etc.) which of course is not possible. I'll update my comment so it's clearer :-) Oh well, I cannot edit the comment since someone has upvoted it I guess. – HaukurHaf Jul 17 '14 at 08:35
  • @HaukurHaf I assumed the OP, saying "I don't want to change all my code" meant they would be happy with light refactoring, such as converting the method into a pure function. But would not want a major rewrite. – Aron Jul 17 '14 at 08:38
  • Thank you everybody. I am learning alot form your comments. I can change my code and it is OK. my problem is how can I pass results stored in local variables to other class. I have big part of code for output and have separated class. By using local variables all those classes are disconnected. – Mehdi Jul 17 '14 at 08:51

1 Answers1

1

Multidimensional arrays are rarely efficient in most programming languages. Try using a jagged array (array of arrays) rather than a multidimensional array.

See http://msdn.microsoft.com/en-us/library/2s05feca.aspx

Ananke
  • 1,250
  • 9
  • 11
  • Why do you think jagged arrays should be faster? They require 2 memory-accesses (for 2d array) while miltidimentional array - only one. – Sasha Jul 17 '14 at 08:24
  • 1
    Only within each loop. You wouldn't need two memory accesses per element. See http://stackoverflow.com/questions/468832/why-are-multi-dimensional-arrays-in-net-slower-than-normal-arrays – Ananke Jul 17 '14 at 08:55
  • A rectangular two-dimensional array requires less memory than a rectangular jagged array. But an array that is not truly rectangular, and has varying subarray lengths, could use less memory as a jagged array. http://www.dotnetperls.com/jagged-2d-array-memory – Mehdi Jul 17 '14 at 09:04