views:

294

answers:

6

I'm looking for a white paper or online book/tutorial about coding efficiently in Java. I've read the white paper from Sun on Performance Tuning (which was mostly about JVM settings) and the one about Garbage Collection management. I'm looking for a paper that focuses more at the code level - which types of constructs are more efficient than others, what code patterns are more efficient than others, and so on.

Any suggestions?

+5  A: 

Joshua Bloch's Effective Java has some performance related information.

Josef
+1 that book is great it tells you a lot of the "gotchas" that some programmers take for granted in java, like the best way to implement equals (and hashcode)
Hardwareguy
As much as I dig this book, and have touted it myself on several occasions, I'm not sure it deserves to be the top answer here. While it's clearly the #1 book any serious Java developer should read, it isn't really a "programming for performance" book. E.g. JLR's answer seems to match the question better.
Jonik
+5  A: 

Have you checked out the Java Performance Tuning book from O'Reilly? It's also available on Safari. It claims to be more code-focused than system-focused.

MattK
It looks like this was written before Java 5, so it won't cover any optimizations related to 5 code, but Effective Java covers most of those.
Kathy Van Stone
Good to know, I guess a combination of those two books will provide most of what I need.
Elie
A: 

Depends on whether you are programming a library or an application:

For libraries I have found Test Driven Development to work very well.

  • FIRST add one or more tests that calls the library code you want to write, and compare the returned value to the desired result.
  • THEN use your IDE to create the actual library code (Ctrl-1 in Eclipse is great). Just empty scaffolds.
  • WHILE test fails DO revise library code UNTIL all tests pass (both your new ones and the previous ones)

In the above, write the simplest possible code that fullfill the tests (as they are your documentation of what the library does). Note that "simplest possible" can be quite complex, but does not have to be.

For applications I have usually found that performance bottlenecks is due to some library code not scaling properly. Use a profiler to identify these library calls, and add a test showing that the scaling is bad - probably by investigating the relative times measured by System.nanoTime() for a "small" and a "large" argument. Then revise again until all tests pass :)

It takes a bit more work up front, but it works well in the long run.


EDIT:

The only advice usually advocated is to be careful about the single overloaded operator in Java, namely string concatenation, where the naive "+=" approach does not scale well for large strings. There are several reasons for that - the primary one is that the JVM's constantly improve and what might be an issue in release 1.4 may be transparently improved to be of no importance in later revisions. A prime example is the HotSpot inlining of small methods, where method calls are simply optimized away. Also differnet JVM's have different weak spots. Only code around those when absolutely necessary.

All the usual tricks for circumventing various JVM limitations usually result in LESS READABLE CODE which goes against the "simplest code possible which does the job".

See "Evaluating Java for Game Development" for a discussion of the performance issues back in 2002. It is a very interesting read.

http://java.coe.psu.ac.th/FreeOnline/Evaluating%20Java%20for%20Game%20Development.pdf

For initial profiling the VisualVM with Java 6 u 10 or later, is quite usable.

Thorbjørn Ravn Andersen
Your answer is good, but I'm downvoting it for the same reason as my comments to @coobird's answer - it doesn't answer the question, which is looking for white papers/online resources.
Rob Hruska
While this strategy works for fixing performance issues as they arise, and for locating non-tuned code, it does not direct me to a resource about known performance issues to keep in mind while programming. The strategy is sound, but it doesn't answer the question.
Elie
The question is wrong, the answer is not reading white papers they dont understand. One needs to first visit the basics and tooling - profilers is the best answer to understanding ones own code in all its glory.Real emperical evidence via real tools rather than theorectical papers would significantly help. The close to the problem one remains rather than being removed can only help. After all the profile will tell your problems about you code, who cares about someone elses.
mP
The question cannot be "wrong" - it's what I want to know. I know how to profile my code, and I'm looking for white papers and books to gain a better understanding of performance and Java. As I said in my previous comment, the answer provided was for a question about improving performance of a Java application, which is not what I asked.
Elie
In that case I suggest you learn "Efficient Java" by heart. It is exactly what you ASK for.
Thorbjørn Ravn Andersen
+1  A: 

There are some things to avoid, but for the most part you really don't want to think about efficiency too much, you want to concentrate on readability.

The Compiler and JIT is going to keep improving and will often out-think even your best optimizations, in fact, some coding "Optimizations" will keep the compiler from optimizing a bit of code, making things much slower.

Never prematurely optimize. If you your specs give performance requirements AND you don't meet them, then you might think of optimizing.

Even most string manipulation isn't as bad as you'd think. Although I don't recommend appending to a string in a loop, arbitrarily switching to a StringBuilder type of object may not gain you a thing in many cases.

By the way, know how to program! Don't do an insertion sort into an ArrayList, don't use a LinkedList if you expect to do indexed lookups a lot, avoid an inner loop if you possibly can ...--stuff like that isn't optimizing, it's just fixing bad code!

Bill K
A: 

I suggest you write a real program and test it with real data, and then use a profiler or two. Trying to predict in advance what you need to tune is difficult if you have been programming in Java for ten years, and you would be surprised how often something comes up which you would never think of.

In short; know where you performance bottlenecks are, don't guess.

Peter Lawrey
I've been programming in Java for 8 years, and I know some of the basic concepts in profiling for performance, and some of the basic mistakes made that can have a major impact on performance. That being said, I'm specifically looking for documented information on performance and Java, not to try to guess the compiler and JIT compiler, but to learn some better coding practices.
Elie
+2  A: 

Two resources, both from Brian Goetz.

  • Brian's monthly column Java Theory and Practice on IBM's developerWorks. I don't know if he still writes it -- last entry was summer 2008. You can find previous columns at the link above or here.

  • His book, Java Concurrency in Practice (with other authors). As the title states, the book focuses on concurrency but don't let that drive you away; in case you think you aren't doing concurrent programming read Chapter 2. The chapter on the Java memory model should be required reading for all Java developers.

I had the pleasure of hearing him speaking about the Java memory model a few years ago at a NFJS conference. Great speaker, great writer. Check his stuff out.

Outside of that, I follow these feeds:

  • JavaSpecialists (archives). Actually an email newsletter. Covers a lot of corner cases and puzzler-type topics.

  • Java Performance Tuning. Another monthly newsletter (via RSS feed, though). More focused on JVM tuning than coding techniques, but a good resource nonetheless.

  • High Scalability. A really informative blog/news site about scalability techniques. Not particular to Java, but since a lot of shops use Java, you'll occasionally get some nice high-level tips.

JLR