Live data from Hacker News

Why is 2 * (i * i) faster than 2 * i * i in Java?

stackoverflow.com

1–10 of 109 posts

Re: Why is 2 * (i * i) faster than 2 * i * i in Java?

#9

That 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?

#10
post #5

You 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.
Post reply on HN