如何优化重复添加

时间:2015-06-08 12:07:14

标签: c

#define NULL 0

int main()
{
    int *array1=NULL,*array2=NULL;
    int x =add(array1[0],array2[0]);
    int y =add(array1[1],array2[7]); 
    int x =add(array1[2],array2[3]);
    int y =add(array1[3],array2[4]);
    int x =add(array1[4],array2[6]);
    int y =add(array1[5],array2[1]); 
    int x =add(array1[6],array2[5]);
    int y =add(array1[7],array2[2]);
    ................
    ................
    int x =add(array1[252],array2[0]);
    int y =add(array1[253],array2[7]); 
    int x =add(array1[254],array2[3]);
    int y =add(array1[255],array2[4]);
}

基本上array1的索引从0开始递增到255 但array2的索引固定为0到7.所以我想优化这个多次加法。如何优化这个?

3 个答案:

答案 0 :(得分:5)

你能做什么

int j = 0,order[] = {0,7,3,4,6,1,5,2};
for(int i = 0;i <256; i +=2)
{
    int x =add(array1[i],array2[order[j%8]]);
    j++;
    int y =add(array1[i+1],array2[order[j%8]]);
    j++;
}

<强>更新
替代解决方案可以是(如果你不想使用i + = 2)

int j = 0,order[] = {0,7,3,4,6,1,5,2};
for(int i = 0;i <256; i ++)
{
    int x =add(array1[i],array2[order[j%8]]);
    j++;
    i++; 
    if(i>=256) break;  //Improves it if you have non even condition
    int y =add(array1[i],array2[order[j%8]]);
    j++;
}

由山姆编辑
现在我想比较x和y的这两个值,并根据比较选择值

     CurrentTre=256;
     if (x > y)
     {
     *array3[0]= x;
     *array4[CurrentTre +0] = 0;
     }
     else
     {
     *array3[i] = y;
     *array4[CurrentTre + 0] = 1;
     }
     ..........
     ..........
     if (x > y)
     {
     *array3[0]= x;
     *array4[CurrentTre +127] = 254;
     }
     else
     {
     *array3[i] = y;
     *array4[CurrentTre + 127] = 255;
     }
     /////////////
     my approach is this way

     if (x > y)
     {
     *array3[i]= x;
     *array4[int CurrentTre +i] = int number[i]<<1;
     }
     else
     {
     array3[i] = y;
     array4[int CurrentTre + i] = int number[i]<<1|1;
     }
} //end function main

我想优化下面给出的优化代码    请检查我是否正确..?

uint32 even_number[255] ={0};
uint32 loop_index1=0;
uint32 loop_index2=0;

uint16 order[256]={0,7,3,4,6,1,5,2,4,3,7,0,1,6,2,5,7,0,4,3,2,5,1,6,3,4,0
,7,6,1,5,2,4,3,7,0,1,6,2,5,0,7,3,4,5,2,6,1,3,4,0,7,6,1,5,2,7,0,4,3,2,5
,1,6,5,2,6,1,0,7,3,4,1,6,2,5,4,3,7,0,2,5,1,6,7,0,4,3,6,1,5,2,3,4,0,7,1
,6,2,5,4,3,7,0,5,2,6,1,0,7,3,4,6,1,5,2,3,4,0,7,2,5,1,6,7,0,4,3,3,4,0,7
,6,1,5,2,7,0,4,3,2,5,1,6,4,3,7,0,1,6,2,5,0,7,3,4,5,2,6,1,7,0,4,3,2,5,1
,6,3,4,0,7,6,1,5,2,0,7,3,4,5,2,6,1,4,3,7,0,1,6,2,5,6,1,5,2,3,4,0,7,2,5
,1,6,7,0,4,3,1,6,2,5,4,3,7,0,5,2,6,1,0,7,3,4,2,5,1,6,7,0,4,3,6,1,5,2,3
,4,0,7,5,2,6,1,0,7,3,4,1,6,2,5,4,3,7,0}; //all 256 values



for(loop_index1;loop_index1<256;loop_index1++)
   {    
    m0= (CurrentState[loop_index1]+Branch[order[loop_index2]]);
loop_index2++;
loop_index1++;
if(loop_index1>=256) 
break;

m1= (CurrentState[loop_index1]+Branch[order[loop_index2]]);

loop_index2++;

if (mo > m1)
 {
 NextState[loop_index1]= m0;
 SurvivorState[CurrentTrellis + loop_index1] =
 even_number[loop_index1]<<1;
 }
else
{
 NextState[loop_index1] = StateMetric1;
 SurvivorState[CurrentTrellis + loop_index1] = 
  even_number[loop_index1<<1|1;
}
}

答案 1 :(得分:1)

第一步:

for (int i = 0; i < 256; i+=8) {
  x = add(array1[i],   array2[0]);
  y = add(array1[i+1], array2[7]);
  ...
}

答案 2 :(得分:0)

使用指针

    int *array1 = NULL, *array2 = NULL;
    int *ptr1 = array1;
    int x = add(*ptr1++, array2[0]);
    int y = add(*ptr1++, array2[7]); 
    int x = add(*ptr1++, array2[3]);
    int y = add(*ptr1++, array2[4]);
    int x = add(*ptr1++, array2[6]);
    int y = add(*ptr1++, array2[1]); 
    int x = add(*ptr1++, array2[5]);
    int y = add(*ptr1++, array2[2]);
    ................
    ................
    int x = add(*ptr1++, array2[0]);
    int y = add(*ptr1++, array2[7]); 
    int x = add(*ptr1++, array2[3]);
    int y = add(*ptr1++, array2[4]);

但请记住 premature optimization is the root of all evil

在优化测量之前,确保您需要优化对阵列的访问。

优化措施后,确保优化有任何效果。