Click here to Skip to main content
15,888,908 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello i have this code:
C#
double c;
       double e;
       double E;
       double o;
       double v;


       public void compute(int[,] arr)
       {
           c = 0;
           e = 0;
           E = 0;
           o = 0;
           v = 0;

           for (int i = 0; i < 256; i++)
               for (int j = 0; j < 256; j++)
               {
                   {

                       c += Math.Pow(i - j, 2) * arr[i, j];
                       e += Math.Pow(arr[i, j], 2);
                       if (arr[i, j] != 0)
                       {
                           E += arr[i, j] * Math.Log(arr[i, j], 2);
                       }
                       o += (arr[i, j] / (1 + Math.Abs(i - j)));
                       v += ((Math.Pow((arr[i, j] - get_p(arr)), 2)) / 256);

                   }
               }

       }
       public double get_p(int[,] arr)
       {
           double p = 0;

           for (int i = 0; i < 256; i++)
               for (int j = 0; j < 256; j++)
               {
                   {


                       p += arr[i, j] / 256;

                   }
               }

           return p;
       }


i have a problem with it, beeing too slow, can someone tell me some tips to make it work faster? or another way to do the same thing that work faster
Posted
Comments
Sander Rossel 25-Feb-12 6:31am    
Perhaps you can give us a clue to what you are trying to achieve?
aryx123 25-Feb-12 6:42am    
e,E,c,o,v are some texture caracteristics of a given image, the array its actualy a coocurence matrix

1 solution

Pre calculate get_p!

You don't modify your array in any way, but each time round each of your nested loops in compute you calculate the same result (via another nested pair of loops)!

Other than that, there are some improvement you can make, but they will probably be fairly minor in comparison. Before you even start on them, you need to time you app accurately and work out the current speed. Then work out which bits are taking time with more timings. Then you have a target to work against.
 
Share this answer
 
Comments
aryx123 25-Feb-12 7:52am    
It was just dumb of me not to see that get_p its calculated again and again for each loop in compute.
Thank you for enlighting me!
OriginalGriff 25-Feb-12 8:28am    
You're welcome!
Sergey Alexandrovich Kryukov 26-Feb-12 2:17am    
My 5. Pretty obvious.
--SA

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