Aren't all of these except for "code motion" (and in some cases the sub-expression trick) compiler optimizations anyway? And as for the manual loop unrolling, try it some time. Branch prediction in 'for' loops is at the point where that little trick buys you absolutely nothing anyway. Logically the unrolling makes sense, but try it some time. Java can iterate over an array of over a million entries in a couple of milliseconds and the unrolled version isn't any faster.
The good portion of this article can be boiled down to 1 thing: don't execute expensive code more than once if you don't have to. The rest the compiler/vm will optimize for you, so don't uglify your code for no reason.
I completely agree with you Rob; but I also think that even you know that many (too many IMHO) people just ignore that rule.
I use to see db queries inside loops with thousands iteration in "some people" 's code, when one proper group by would do the same; who does such shameful things is beyond any hope :-(
The common sub-expression stuff and the moving statements outside of loops is obvious, the rest is dubious premature optimisation best left to the JIT compiler. Even the article itself says the loop unrolling is pointless.
If this article is going to suggests these kind of optimisations, it needs to provide some hard evidence (benchmarks) of their benefit.
These optimizations are all on a very low level, so low in fact that I would not bother to think much about them, expecting that the JIT compiler would take care of most of them. Don't optimize unnecessarily if you don't know that you have to. First strive to get your program to work correctly. These kinds of low-level optimizations can be more distracting to novices than helpful, and all of them are already well known to more advanced programmers.
Even moving expressions outside a loop doesn't always provide a performance advantage. Loop Hoisting is a compiler optimization that moves expressions outside a loop. So this could also be a task of the JIT.
,
Comments
Rob Signorelli replied ago:
Aren't all of these except for "code motion" (and in some cases the sub-expression trick) compiler optimizations anyway? And as for the manual loop unrolling, try it some time. Branch prediction in 'for' loops is at the point where that little trick buys you absolutely nothing anyway. Logically the unrolling makes sense, but try it some time. Java can iterate over an array of over a million entries in a couple of milliseconds and the unrolled version isn't any faster.
The good portion of this article can be boiled down to 1 thing: don't execute expensive code more than once if you don't have to. The rest the compiler/vm will optimize for you, so don't uglify your code for no reason.
pentolino replied ago:
I completely agree with you Rob; but I also think that even you know that many (too many IMHO) people just ignore that rule.
I use to see db queries inside loops with thousands iteration in "some people" 's code, when one proper group by would do the same; who does such shameful things is beyond any hope :-(
Daniel Dyer replied ago:
The common sub-expression stuff and the moving statements outside of loops is obvious, the rest is dubious premature optimisation best left to the JIT compiler. Even the article itself says the loop unrolling is pointless.
If this article is going to suggests these kind of optimisations, it needs to provide some hard evidence (benchmarks) of their benefit.
htowninsomniac replied ago:
These optimizations are all on a very low level, so low in fact that I would not bother to think much about them, expecting that the JIT compiler would take care of most of them. Don't optimize unnecessarily if you don't know that you have to. First strive to get your program to work correctly. These kinds of low-level optimizations can be more distracting to novices than helpful, and all of them are already well known to more advanced programmers.
alarmnummer replied ago:
Even moving expressions outside a loop doesn't always provide a performance advantage. Loop Hoisting is a compiler optimization that moves expressions outside a loop. So this could also be a task of the JIT.
,
Ignacio Coloma replied ago:
Bit shifts instead of multiplication is also an old-old-old technique that is dubiously applicable to modern microprocessors.
anithahot replied ago:
A really golden article for optimizing java applications! Great going!
Voters For This Link (7)
Voters Against This Link (15)