Live data from Hacker News

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

stackoverflow.com

71–80 of 109 posts

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

#71

With all the optimisations being implemented in compilers today, it is impressive to see how this opportunity to optimise is missed. Put differently, compiler writers bother about optimisations that gain 0.1% performance in some special cases, but others that could gain 20% performance are not implemented. Why? Is this optimisation particularly difficult to implement? Or is it just missed low-hanging fruit? It sure l…

It's possible that they're working in the frame of mind that there aren't any low-hanging fruit left after so many years of compiler optimizations and forget to even try.

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

#72
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

Updated version that specifies -fwrapv on the commandline to turn the integer overflow into defined behavior: https://godbolt.org/z/K-Ijl0

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

#73
post #25

Earlier quoted context omitted.

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.

So if the compiler is too good you want to trick it to produce less optimal code so you can benchmark it fairly? Isn't it part of the benchmark to allow the compiler reduce the whole expression to a compile time constant?

Aside from what the sibling says about the difference between the test harness code and the code being benchmarked, there's a more abstract point: you want the compiler to reduce it to a compile time constant if and only if in the real world cases you're trying to model, it will be able to do so. That's pretty rare, since if that happens, you probably wouldn't have to do performance analysis on that code.

These days I find myself telling people that benchmark numbers don’t matter on their own. It’s important what models you derive from those numbers. Refined performance models are by far the noblest and greatest achievement one could get with the benchmarking — it contributes to understanding how computers, runtimes, libraries, and user code work together. --Aleksey Shipilёv https://shipilev.net/blog/2014/nanotrusting-nanotime/

That's a bit of an obscure comment, but I keep coming back to it as I learn about performance work and benchmarking.

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

#74

With all the optimisations being implemented in compilers today, it is impressive to see how this opportunity to optimise is missed. Put differently, compiler writers bother about optimisations that gain 0.1% performance in some special cases, but others that could gain 20% performance are not implemented. Why? Is this optimisation particularly difficult to implement? Or is it just missed low-hanging fruit? It sure l…

Compiler developers have tons of benchmarks which they run. I’d bet that this is as simple as not being significant in their test suite, with a good chance that it’s both not as simple as it might seem or that there are impacts on more complicated code which is in their benchmark suite or a big customer’s app.

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

#75
post #35

Earlier quoted context omitted.

However, if you look at the second, you won't see any left shifts, which is also interesting

I find it weird that he doesn't mention this difference as part of the performance difference. A left shift should be considerably faster than a mul operation?

I don't think this is generally true on modern processors.

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

#76
post #19

Earlier quoted context omitted.

I find it interesting that there are developers out there that know to look at these nuances when respond to Stack Overflow questions. I'm been developing professionally for 10 years and probably went over branch prediction in my computer architecture class in college (I'm guessing I did, if I didn't then I never encountered it at all!). The person who answered the multiple question dove into byte code...but also ans…

I don't see anything regarding Angular, they're obviously very knowledgeable, but it's pretty much focused on low-level: https://stackoverflow.com/users/922184/mysticial?tab=tags&so...

I think these are his responses

https://stackoverflow.com/users/485343/rustyx

He answered a question on ticking clock in Angular.

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

#77
post #76

Earlier quoted context omitted.

I don't see anything regarding Angular, they're obviously very knowledgeable, but it's pretty much focused on low-level: https://stackoverflow.com/users/922184/mysticial?tab=tags&so...

I think these are his responses https://stackoverflow.com/users/485343/rustyx He answered a question on ticking clock in Angular.

Oh, my mistake, I thought we were talking about the question linked by azhenley above.

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

#79
post #35

Earlier quoted context omitted.

However, if you look at the second, you won't see any left shifts, which is also interesting

I find it weird that he doesn't mention this difference as part of the performance difference. A left shift should be considerably faster than a mul operation?

I believe this is far less true than it used to be, but it’s a good example of why these decisions really need to be data driven as compilers and processors change faster than most people can afford to optimize code. I don’t know that this would be the case for something that simple but I’ve seen a fair amount of heavily-tuned C/ASM code which was replaced with the now-faster “reference” code when someone noticed that the old assumptions weren’t true.
Post reply on HN