Link Details

Link 82972 thumbnail
User 290950 avatar

By veerakm
via javabyexample.wisdomplug.com
Submitted: May 26 2008 / 10:32

some optimizations that you can do to your code. These optimizations are general and apply to any programming language.
  • 8
  • 15
  • 1500
  • 758

Comments

Add your comment
User 282119 avatar

Rob Signorelli replied ago:

4 votes Vote down Vote up Reply

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.

User 221312 avatar

pentolino replied ago:

3 votes Vote down Vote up Reply

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 :-(

User 172008 avatar

Daniel Dyer replied ago:

2 votes Vote down Vote up Reply

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.

User 272422 avatar

htowninsomniac replied ago:

0 votes Vote down Vote up Reply

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.

User 160967 avatar

alarmnummer replied ago:

0 votes Vote down Vote up Reply

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.
,

User 204561 avatar

Ignacio Coloma replied ago:

1 votes Vote down Vote up Reply

Bit shifts instead of multiplication is also an old-old-old technique that is dubiously applicable to modern microprocessors.

User 307955 avatar

anithahot replied ago:

0 votes Vote down Vote up Reply

A really golden article for optimizing java applications! Great going!

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.