I thought at first this was because integer squaring is potentially faster than general integer multiplication and the compiler wasn't seeing the square operation in the second case, but that's not the explanation here.
Why is 2 * (i * i) faster than 2 * i * i in Java?
21–30 of 109 posts
Re: Why is 2 * (i * i) faster than 2 * i * i in Java?
#22That just might be the most dedicated answer I've ever seen on Stack Overflow.
It is a good answer, but my favorite by far is an answer about branch prediction to explain why processing a sorted array is faster than unsorted: https://stackoverflow.com/q/11227809/938695
Re: Why is 2 * (i * i) faster than 2 * i * i in Java?
#23You should translate your program to C++ and build with clang ; it turns the loop into a single constant load. https://godbolt.org/z/slznbU
Did you read TFA? The author did that (though using GCC), and the reason the optimizer does what you see is undefined behavior due to signed integer overflow.
Just to be clear, undefined behavior means the standard allows implementations to do what they they feel is the right thing to do under that scenario, and the outcome will still comply with the standard.
Re: Why is 2 * (i * i) faster than 2 * i * i in Java?
#24So it's an issue of the optimizer; as is often the case, it unrolls too aggressively and shoots itself in the foot, all the while missing out on various other opportunities. In my experience, loop unrolling should basically never be done except in extremely degenerate cases; I remember not long ago someone I know who also optimises Asm remarking "it should've died along with the RISC fad". The original goal was to re…
In most cases on modern systems, small loops should remain compact as possible, to stay in the uop cache. The "for" loop overhead (the inc, cmp, and jmp instructions) effectively execute in parallel. Modern systems are highly out-of-order and the for-loop overhead is virtually nil.
Re: Why is 2 * (i * i) faster than 2 * i * i in Java?
#25You should translate your program to C++ and build with clang ; it turns the loop into a single constant load. https://godbolt.org/z/slznbU
It's usually a good idea to turn loop bound into a variable when benchmarking a compiler, lest it optimizes the whole thing away like in this case.
Re: Why is 2 * (i * i) faster than 2 * i * i in Java?
#26You should translate your program to C++ and build with clang ; it turns the loop into a single constant load. https://godbolt.org/z/slznbU
Did you read TFA? The author did that (though using GCC), and the reason the optimizer does what you see is undefined behavior due to signed integer overflow.
> the reason the optimizer does what you see is undefined behavior due to signed integer overflow
Yes undefined behavior gives the optimizer the right in this case to transform the code into anything, including a nonsense answer, or a trap instruction. But the optimizer did not; it produced the right answer under 2's complement arithmetic.
Re: Why is 2 * (i * i) faster than 2 * i * i in Java?
#27Earlier quoted context omitted.
Did you read TFA? The author did that (though using GCC), and the reason the optimizer does what you see is undefined behavior due to signed integer overflow.
Did you understand the comment? The author used GCC, and GCC is only able to vectorize the loop. But clang on the other hand, essentially turned this O(n) algorithm to calculate a particular sum into an O(1) result. > the reason the optimizer does what you see is undefined behavior due to signed integer overflow Yes undefined behavior gives the optimizer the right in this case to transform the code into anything, inc…
Re: Why is 2 * (i * i) faster than 2 * i * i in Java?
#28Re: Why is 2 * (i * i) faster than 2 * i * i in Java?
#29TIL about printing ASM from debug JVMs.
https://www.youtube.com/watch?v=_cFwDnKvgfw
There are also other tools like JITWatch.
https://github.com/AdoptOpenJDK/jitwatch/wiki/Videos-and-Sli...
Re: Why is 2 * (i * i) faster than 2 * i * i in Java?
#30So it's an issue of the optimizer; as is often the case, it unrolls too aggressively and shoots itself in the foot, all the while missing out on various other opportunities. In my experience, loop unrolling should basically never be done except in extremely degenerate cases; I remember not long ago someone I know who also optimises Asm remarking "it should've died along with the RISC fad". The original goal was to re…