-3
#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]);
}

Basically index for array1 is incrementing by 1 starting from 0 to till 255 but the index for array2 is fixed from 0 to 7. So I want to optimize this multiple addition. How to optimize this?

4
  • 5
    Your code doesn't really make sense since you seem to be redefining x and y.
    – cnicutar
    Commented Jun 8, 2015 at 12:10
  • 5
    yeah, remove all the previous lines in main() except the last two. done. Commented Jun 8, 2015 at 12:10
  • accessing array[1] will crash Commented Jun 8, 2015 at 12:11
  • 4
    @PeterMiehle: If that was guaranteed by UB to crash, it would be better. But it is not. Note that already array_[0] is UB btw (dereference null pointer). Commented Jun 8, 2015 at 12:13

3 Answers 3

5

what you can is

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++;
}

UPDATE
alternate solution can be (if you want without using 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++;
}

edit by sam
Now i want to compare this two values of x and y and selecting value based on comparision

     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

I want to optimize the code my optimization is given below please check whether am i doing right or not..?

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;
}
}
15
  • 1
    y=array1[i+1] and for(...,i+=2) Commented Jun 8, 2015 at 12:23
  • @PeterMiehle I fixed it too, but since number is even, your way is better
    – Varun Garg
    Commented Jun 8, 2015 at 12:27
  • @Lundin ok, but for y, you cant simply use index2+1 as index of second array you need to get it back from order.
    – Varun Garg
    Commented Jun 8, 2015 at 12:36
  • why i is incremented by 2..?
    – sam
    Commented Jun 9, 2015 at 4:44
  • @sam example a begining i = 0, for x we are using 0, but in the same loop, for y we are putting i+1 which is 1 as needed, now if we increment only by 1 x will be using 1, but we need it it to be 2, hence we increment it by 2 as by using array[i+1] does not increment i.
    – Varun Garg
    Commented Jun 9, 2015 at 5:20
1

first step:

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

Use a pointer

    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]);

But remember premature optimization is the root of all evil.

Before optimizing measure and make certain you need optimizing the accesses to the array.

After optimizing measure to make certain the optimization had any effect.

Not the answer you're looking for? Browse other questions tagged or ask your own question.