This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TDA4VM: 变量索引和常量索引速度差别很大?

Part Number: TDA4VM


你好,请教一下,下面代码的速度差别很大:

代码一:

const int a=b;

group[a]++;

代码二:

int a=1;

group[a]++;

请问是什么原因导致第二段代码比第一段快很多?是否和编译器流水线的设置有关?

  • 代码一用const定义的变量放在.const段,这个段是放在外部flash或ROM,所以要从外部memory去读取,速度比较慢。 Int定义的变量放在.bss段,一般放在片上RAM,读取速度快。请看下面的文档7.5.2 The const Keyword
    https://www.ti.com/lit/ug/sprui04b/sprui04b.pdf

  • for(col=out_pitch+1;col<_mpy((edge_blk_y-1),out_pitch)-3;col++)
    {

        if(((temp32_2&0x20000)!=0x20000)&&((temp32_1&0x2000000)==0x2000000||(temp32_1&0x20000)==0x20000||(temp32_1&0x200)==0x200||(temp32_2&0x2000000)==0x2000000||(temp32_2&0x200)==0x200||(temp32_3&0x2000000)==0x2000000||(temp32_3&0x20000)==0x20000||(temp32_3&0x200)==0x200))
            pOutEdgeL2[_mpy((col/out_pitch),out_width)+col%out_pitch]=((temp32_2&0x20000)>>16)+1;

        temp_1_1=temp_1_2;       temp_1_2=temp_1_3;         temp_1_3=pInL2[col-out_pitch+2];
        temp_2_1=temp_2_2;       temp_2_2=temp_2_3;         temp_2_3=pInL2[col+2];
        temp_3_1=temp_3_2;       temp_3_2=temp_3_3;         temp_3_3=pInL2[col+out_pitch+2];

    }

    上面的out_pitch如果定义成宏或者替换为常数,运行速度明显加快,如果定义成变量,速度慢很多,是什么原因造成的?

  • 您可以比较一下两种情况的汇编指令有什么不一样的地方。