To speed up my image processing application, I'm trying to address values in an array starting in the middle, and using indices starting at 0.
For example, when I work with two arrays, I often have a situation like this:
public void work(int[] array1, int[] array2, int offset)
{
int len = array1.length;
for (int i = 0; i < len; i++)
array1[i] += array2[i + offset];
}
so I would like to create a new variable, array3 that directly maps into the middle of array2 (not a copy), so I can do this:
public void work(int[] array1, int[] array2, int offset)
{
int[] array3 = array2[offset]...;
int len = array1.length;
for (int i = 0; i < len; i++)
array1[i] += array3[i];
}
In effect, I want a Java equivalent to this c statement:
int *array3ptr = array2ptr + offset;
Note: I don't have any experience with JNI or anything, so if it boils down to using this, please provide working examples.
This is not possible to do by using pure Java arrays and the array[index] notation. In Java arrays can never overlap.
You could however wrap your arrays in IntBuffer. If you use a direct buffer (see allocateDirect) you should get real good performance.
A statement such as
int *array3ptr = array2ptr + offset;
would be written as
IntBuffer array3 = array2.slice().position(offset);
These
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With