处理数组太慢的问题 [英] too slow problem working with array

查看:142
本文介绍了处理数组太慢的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有以下代码:

Hello i have this code:

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

推荐答案

预先计算get_p

您不会以任何方式修改数组,但是每次围绕compute中的每个嵌套循环,您都会计算出相同的结果(通过另一对嵌套循环)!

除此之外,您可以进行一些改进,但是相比之下,它们可能会很小.在开始使用它们之前,您需要准确地为应用计时,并确定当前速度.然后确定哪些位需要更多的时间.然后,您有一个目标可以应对.
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.


这篇关于处理数组太慢的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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