1

So for my research group I am attempting to convert some old C++ code to Java and am running into an issue where in the C++ code it does the following:

    method(array+i, other parameters)

Now I know that Java does not support pointer arithmetic, so I got around this by copying the subarray from array+i to the end of array into a new array, but this causes the code to run horribly slow (I.e. 100x slower than the C++ version). Is there a way to get around this? I saw someone mention a built-in method on here, but is that any faster?

3
  • 1
    Are you using System.arraycopy()? Commented Sep 30, 2014 at 18:00
  • No, I had just been manually copying the subarray into a new one. Is System.arraycopy() significantly fasteer? Commented Sep 30, 2014 at 18:02
  • 1
    As the answers indicate, the equivalent Java idiom is to pass the a reference to the original array along with a start index. The core JDK classes are full of examples of such usage - many methods that accept an array are overloaded to also accept an array and a start/end index to operate on a slice of the array. See for example String(char[] value) vs. String(char[] value, int offset, int count) Commented Sep 30, 2014 at 18:11

4 Answers 4

6

Not only does your code become slower, it also changes the semantic of what is happening: when you make a call in C++, no array copying is done, so any change the method may apply to the array is happening in the original, not in the throw-away copy.

To achieve the same effect in Java change the signature of your function as follows:

void method(array, offset, other parameters)

Now the caller has to pass the position in the array that the method should consider the "virtual zero" of the array. In other words, instead of writing something like

for (int i = 0 ; i != N ; i++)
    ...

you would have to write

for (int i = offset ; i != offset+N ; i++)
    ...

This would preserve the C++ semantic of passing an array to a member function.

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

Comments

2

The C++ function probably relied on processing from the beginning of the array. In Java it should be configured to run from an offset into the array so the array doesn't need to be copied. Copying the array, even with System.arraycopy, would take a significant amount of time.

It could be defined as a Java method with something like this:

void method(<somearraytype> array, int offset, other parameters)

Then the method would start at the offset into the array, and it would be called something like this:

method(array, i, other parameters);

Comments

0

If you wish to pass a sub-array to a method, an alternative to copying the sub-array into a new array would be to pass the entire array with an additional offset parameter that indicates the first relevant index of the array. This would require changes in the implementation of method, but if performance is an issue, that's probably the most efficient way.

Comments

0

The right way to handle this is to refactor the method, to take signature

method(int[] array, int i, other parameters)

so that you pass the whole array (by reference), and then tell the method where to start its processing from. Then you don't need to do any copying.

Comments

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.