1

I would like to know, which is fast way to write/read in an int array.

Here my Java code: I have three int arrays, two in read access and one int array in write access.

for(int j = h20 ; j < h21 ; j++){
    for(int i = w20 ; i < w21 ; i++){
        if( int_color == arr3[j*h31 + i] )  continue; //condition 
        arr1[(j+decY)*w11 + i+decX] = arr2[j*w21 + i];      
    }   
}

My code is a classic 2D array loop, there are just one special condition to check.

Is it other way to write this code to decrease processing time?

Thx.

4
  • "Is it other way to write this code to decrease processing time?" since you need both i and j, even if you can merge your loops, you still need h21 * w21 iteration. SO it won't change much. Now, based on the logic (we don't have all the data here) maybe you can simplify this. Commented Dec 8, 2017 at 9:33
  • Unless you find a way to reduce the number of operations in the calculation of the indices, I'd say this is as optimized as it gets. You could do all operations that rely on j before you enter second loop, so that those values have to be calculated only once per inner loop. Commented Dec 8, 2017 at 9:34
  • You should invert the condition of the if statement and remove continue. Commented Dec 8, 2017 at 9:46
  • No, is impossible to change logic, I know problem is the condition which is a barrier to global memory copy. Commented Dec 8, 2017 at 9:54

2 Answers 2

2

You can reduce the amount of calculations, if you separate them by variables. In your case, any calculation that relies on j alone doesn't have to be inside the inner loop because the result won't change for the rest of the loop. Instead, calculate the values outside and only use the result in the inner loop.

for(int j = h20 ; j < h21 ; j++){
    int tmp1 = j*h31;
    int tmp2 = (j+decY)*w11 + decX;
    int tmp3 = j*w21;

    // j won't change inside here, so you can simply use the precalculated values
    for(int i = w20 ; i < w21 ; i++){
        if( int_color == arr3[tmp1 + i] )  continue; //condition 
        arr1[tmp2 + i] = arr2[tmp3 + i];      
    }   
}

Edit: If you want to reduce this even more, you could rewrite the calculation for tmp2:

(j+decY)*w11 + decX ==> j*w11 + decY*w11 + decX

Then, you could extract the decY*w11 + decX into its own variable outside the first loop.

int tmp0 = decY*w11 + decX;
for(int j = h20 ; j < h21 ; j++){
    int tmp1 = j*h31;
    int tmp2 = j*w11 + tmp0;
    int tmp3 = j*w21;

    // j won't change inside here, so you can simply use the precalculated values
    for(int i = w20 ; i < w21 ; i++){
        if( int_color == arr3[tmp1 + i] )  continue; //condition 
        arr1[tmp2 + i] = arr2[tmp3 + i];      
    }   
}

But this will save you only one addition per iteration, so I don't think it's worth the extra effort.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, you right, I forgot this way. I am not specialist in JAVA, I thought expert method as iterator or other way focused on JAVA methods.
Your optimizations seem to be alright, but they're probably useless as the JIT compiler knows these tricks, too.
1

Removing calculations, especially multiplications might help.

For arr3 this would be:

final int icolor = int_color;
final int ix3 = h20 + w20;
final int dx3 = h31 + h21 - h20;

for (int j = h20; j < h21; ++j) {
    for (int i = w20 ; i < w21 ; ++i) {
        assert ix3 == j*h31 + i;
        if (icolor != arr3[ix3]) {
            arr1[(j+decY)*w11 + i+decX] = arr2[j*w21 + i];
        }
        ++ix3;
    }
    ix3 += dx3;
}

Whether this is really worthwile one needs to test.

Depending on the frequency of the condition, one might think of using System.arraycopy for consecutive ranges of i.

2 Comments

your ix3 was j * h31 + i, you can't evaluate it outside of the loop, you need i and j. Here you are checking each time the same value.
@AxelH right, I had forgotten to increase ix3 in the inner loop. Corrected

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.