Why is 2 * (i * i) faster than 2 * i * i in Java?
stackoverflow.com
Why is 2 * (i * i) faster than 2 * i * i in Java?
1–10 of 109 posts
Re: Why is 2 * (i * i) faster than 2 * i * i in Java?
#2That just might be the most dedicated answer I've ever seen on Stack Overflow.
Re: Why is 2 * (i * i) faster than 2 * i * i in Java?
#3TIL about printing ASM from debug JVMs.
Re: Why is 2 * (i * i) faster than 2 * i * i in Java?
#4I'm surprised it's not doing a left shift for the x2.
Re: Why is 2 * (i * i) faster than 2 * i * i in Java?
#5You should translate your program to C++ and build with clang ; it turns the loop into a single constant load. https://godbolt.org/z/slznbU
Re: Why is 2 * (i * i) faster than 2 * i * i in Java?
#6I'm surprised it's not doing a left shift for the x2.
It is in the first example (the sal instruction)
Re: Why is 2 * (i * i) faster than 2 * i * i in Java?
#7I'm surprised it's not doing a left shift for the x2.
[deleted]
Re: Why is 2 * (i * i) faster than 2 * i * i in Java?
#8I guess they do not use value numbering, which is typically how you get equivalent results for cases like this.
Re: Why is 2 * (i * i) faster than 2 * i * i in Java?
#9That 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?
#10You 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.