Equivalent to C / C ++ pointer increment in Java?

I have some feeling that people will try to beat me on the head and say: "You should not do this!" (and I am fully aware of this fact); however, here is my problem:

Suppose you have the following C ++ code:

EDIT: I think I have not shown enough of what I'm trying to do, although most of the answers were excellent. So here is the edited version of my code. The main thing is that at each iteration I still need to use an array of vales values, which is a submatrix of x , and not just one value in the index:

 unsigned int n = 2000; // or any other number for that matter double * x = new double[n]; double resultArray; // fill array with meaningful data // do all kinds of initializations for( int i=0 ; i<someNumber ; ++i ){ //someNumber ~ 1900 // do some stuff for (j=0 ; j<someSmallerNumber ; ++j){ //someSmallerNumber ~ 15 resultArray[j] = x[j] * someFunction(resultArray[j]); } ++x; //increment pointer to the first element of the array // do some more stuff } 

EDIT: The reason I use ++x in the outer loop is because if I were to use x[outerIndex+innerIndex] instead of just x[innerIndex] , I would still have many more additions to iterate, and this is a heavy algorithm algebra, so I'm trying to squeeze some performance out of it.

I would like to point out that my algorithm never tries to read non-existent memory. This is just processing speed when I try not to recount the current array index more times than I need. This works great, and I am pleased with that.

Now I am trying to implement a similar algorithm in Java (and Java in not quite my area of ​​expertise). However, I know that Java really does not give you access to pointers and treats everything as an object. My question, of course, is an efficient way to execute the equivalent of ++x , where x is the address of the first element of the array in Java? Is it possible to remove the first element of the array? Are there any good quick search algorithms if they are not part of the built-in manipulation of Java arrays?

Thanks in advance! =)

+4
source share
5 answers

Java is not C ++. What you do in C ++ for speed is not necessarily Java. This is one such case.

Using for (int val: x) in Java will not, as some people claim, give you an edge:

 for(int i = 0; i < x.length; i++) { int val = x[i]; } 

Link here; http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.14.2

What people say here about the virtual machine freely makes runtime optimization right.

So, yes, you need to beat a little over your head :-). Although Java and C ++ look the same, what you do in one is not what you would do in another in all cases: to write good Java, you have to think about Java; for C ++, you should think in terms of C ++.

+4
source

Use a for-each : for (double d : theArray) loop for (double d : theArray) - this is an idiomatic way to do this, and this gives the VM the most obvious optimization possibilities.

If your interruption condition depends on the current position in the array, you are probably best off using the classic for loop with a hand-written counter - sorry.

+4
source

An effective way is to use the index and increase it in a loop.

JIT has the right to check the limits of rise beyond the limits of the cycle if it can prove its own satisfaction that the index variable simply covers the range in an orderly way. In your case, the JIT may be difficult to prove that someSmallerNumber always small enough, but it can still go beyond the inner loop. after he checked someSmallerNumber once on the outer loop.

In the simplest case with the so-called "for-each", even if the compiler emits a bytecode that uses an internal index (I don’t know if it does it or not), then the JIT should immediately see that the index can never go beyond .

if I used x [externalIndex + innerIndex] instead of just x [innerIndex] I would have to perform many more additions per iteration

There is no guarantee that JIT is dumber than you - it can hold reused partial computation values ​​in registers if it wants. I'm afraid I don't know if, in practice, JIT will detect that x[outerIndex+innerIndex] can be computed differently by reusing the address in the middle of the array. Reusing a value is not a GC-capable pointer, adds complication, blah blah, so it may or may not be exactly the same optimization as in C ++. But a good optimizer in C ++ is probably able to do the optimization without your help, so I would not be surprised if JIT can too.

+3
source

Short answer: no direct equivalent.

Use indexes or use for (double d : array_of_double) - the for construct is easier to optimize the VM.

+2
source

The idiom I often met often reads the value from the array and stores it in a local variable, processes this local variable and finally saves the result back:

 for (int i = 0; i < someNumber; ++i) { double val = x[i]; // do heavy lifting with val x[i] = val; } 

Of course, if you are only reading from an array, you do not need to write the value back. In this case, just use the for-each loop:

 for (double val : x) { // ... } 
0
source

Source: https://habr.com/ru/post/1343638/


All Articles