×

Loading...

Topic

This topic has been archived. It cannot be replied.
  • 工作学习 / IT杂谈 / 请问用VC++编程时,如果用到new来进行内存的分配,是否特别影响执行速度。我程序中的数组原先是30*30的,不用内存分配即可执行。可现在改成80*80的,
    就必须要用到内存分配,否则出错。原先运行程序只需要40多秒,可现在居然要一个半小时,太夸张了吧。请问会有这么大的差距吗?另外,如何能够提高执行的速度,恢复到原来的水平?谢谢。
    • 你去chinasmile发巴, 我在这里不回答任何技术问题,
    • 你的内存不够了吧. 既然直接执行出错,改用分配内存就可以了, 很可能是你的内存没有80*80个元素那么多. 改用分配内存后,大概是由于运算的需要, 数据在内存与硬盘之间换来换去,就会慢.
      如果是这样, 试试优化你的算法, 争取某些数据运算过之后就不需要再读了.
      • thanks.
    • you should compare 30*30 with/without using new/delete first.
      • what do you mean? when I use 30*30, it doesn\
      • what do you mean?When I used 30*30,
        it doesn\
      • sorry, there is something wrong with my computer. I mean when I used 30*30, it ok. But when it change to be 80*80, it can\
        • 如果你想要知道是不是new/delete让你的速度降低,你得用new申请30*30的内存的速度和直接用数组的速度比吧,还不然没法比压。
          • I tried and it\
          • It\
          • it's ok with30*30.
            • what you mean of "it is ok"
              • that means the speed is the same as before(not adding new/delete).Please help me.. What can I do now?
                • so this problem has nothing to do with new/delete, it comes from your algorithm. use some analysis tools to check which function take most of the time, change it.
                  • how about add some memory into my computer?
                    • sure. It'll help. But keep in mind that your new 80*80 is about 7 times of 30*30. Try to increase the dimension gradually to find out about how much memory you need.
                      • 80*80 = 6.4k... 我怎么觉得6.4k的数组可以直接声明的压。上限不是64k吗? 你的时间变长和NEW没关系,如果用VC,选ENABLE PROFILE,看看各个函数占的时间再说。
                        • 我也觉得和NEW本身没关系. 好像也不是声明的问题吧.不知道他的每个元素有多大? 如果每个元素占10BYTES, 整个数组要用64K MEMORY.
                        • How toselect Enable Profile. Where?Thanks
      • scorpio,请看这里。事实上,我用的数组是80*80*372,请问什么是enable profile?我的程序中只有两个四层循环非常占用时间,别的都没什么。
        • do u mind to post ur source code here?
          • it's very long,do you mind ?
          • 3ìDòêμÔúì«3¤áË£¬Îò¾í°Ñ1ؼüμÄÄÇ2¿·Y¼ÆËãìù3öà′£¬¶àD»ÄãáË¡£
            ÆäÖD£¬s1oís2¾ùÎa80*80μÄêy×é¡£Dèòa¼ÆËãμÄ1«ê½êÇ£o
            s2[p][q]=max(max(w[p-i,q-j]*s1[i][j]))
            ÆäÖDp,q,i,j¶¼êÇ′ó0μ½79
            w[m,n]=exp(-beita^2(m^2+n^2))

            3ìDòèçÏ£¬òѾ-êÔ׿õéù¼ÆËãá¿áË¡£


            NN=40;
            for (i=0;i<inputN1;i=i+1)
            {
            for (j=0;j<inputN1;j=j+1)
            {
            nb1=j-NN;
            if (nb1<0)
            nb1=0;
            nb2=j+NN;
            if (nb2>inputN1)
            nb2=inputN1;

            nb3=i-NN;
            if (nb3<0)
            nb3=0;
            nb4=i+NN;
            if (nb4>inputN1)
            nb4=inputN1;

            aa=0;
            for (i1=nb3;i1<nb4;i1=i1+1)
            {
            for (j1=nb1;j1<nb2;j1=j1+1)
            {
            j2=abs(i-i1);
            j3=abs(j-j1);
            fai=exp(-beita*(j2*j2+j3*j3));
            bb=fai*s1[i1][j1];
            if(aa<bb) aa=bb;
            }
            }
            s2[i][j]=aa;
            }
            }
            • luan ma.
              • 由于程序有8页,我只把计算的那部份贴出来。非常感谢你。
                本文发表在 rolia.net 枫下论坛首先s1,s2是80*80的数组。inputN1=80,beita=0.3,其余均为变量
                需要计算的公式是:
                s2[p][q]=max(max(w(p-i,q-j)*s1[i][j]))
                p,q,i,j from 0 to 79
                w(m,n)=exp(-beita^2(m^2+n^2))
                m,n from -79 to 79

                程序如下,已经进行了简化。
                真正的计算是从第20行开始,前面都是为了简化计算。
                我想问题就出在这里,我试着把这段程序用s2[i][j]=s1[i][j]代替,速度巨快。
                不知道有什么办法能够优化这段程序呢?请一定赐教。谢谢。

                1 NN=40;
                2 for (i=0;i<inputN1;i=i+1)
                3 {
                4 for (j=0;j<inputN1;j=j+1)
                5 {
                6 nb1=j-NN;
                7 if (nb1<0)
                8 nb1=0;
                9 nb2=j+NN;
                10 if (nb2>inputN1)
                11 nb2=inputN1;
                12
                13 nb3=i-NN;
                14 if (nb3<0)
                15 nb3=0;
                16 nb4=i+NN;
                17 if (nb4>inputN1)
                18 nb4=inputN1;
                19
                20 aa=0;
                21 for (i1=nb3;i1<nb4;i1=i1+1)
                22 {
                23 for (j1=nb1;j1<nb2;j1=j1+1)
                24 {
                25 j2=abs(i-i1);
                26 j3=abs(j-j1);
                27 fai=exp(-beita*(j2*j2+j3*j3));
                28 bb=fai*s1[i1][j1];
                29 if(aa<bb) aa=bb;
                30 }
                31 }
                32 s2[i][j]=aa;
                33 }
                34 }更多精彩文章及讨论,请光临枫下论坛 rolia.net