Thursday, July 10, 2008

Top Ten Performance Problems and Their Solutions Submitted

Whether you're the developer or the user of a Java application you would like to see running faster, here are the top ten tips to use.

10) GregorianCalendar

This class is slow and is not synchronized. If possible avoid it and use Joda time

9) No time based animation

All animation should be time based so that if it's meant to be 1 second it happens in 1 second even if it's only 4 frames a second.
I hate the "all download have been finished" window that takes 10 seconds and uses the rest of the CPU available. Look at the timing framework.

8) No cache

Computing the same thing again and again, try to identify where it happens and cache the latest results.
Be careful not to create a memory problem when never releasing objects of the cache.

7) No feedback

If you don't provide a progress bar or a hourglass the user will think the application hangs or is slow. Provide some distraction, he won't notice it or at least he will know what it's doing. Use the JProgressBar or setCursor(new Cursor(Cursor.WAIT_CURSOR));

6) Logging

Logging is slow as it uses the disk and is synchronized (each thread needs to wait its turn).
The first fix is to log on a separate disk than the application is running.
You should also look at what you're logging and reduce it to only what is needed.

5) Premature optimization

What? Yes premature optimization is the root of all evil.
Moreover you may make it worst with incorrect assumptions.
I once profiled a framework where 40% of the time was spent on premature optimization.
Don't assume but measure and act. Measure and optimize during the late Beta phase of your project.
Profilers are your friends here.

4) Database

Database are often the weakest link of an application.
Measure, create indexes where needed, optimize queries and if still a problem change of database.

3) Network

Network is slow. Two things are slow, the connection to the server and the download of the information.
Cache all that come from the network when possible. Compress the information sent and received using GZipOutputStream or a GZip Servlet filter.

2) Software

Use the latest softwares available, especially Java 6 update 6.
Application servers and databases also compete between them to be the fastest. So the more recent version you have the faster it will be.
Another good example of performance improvement is FreeBSD 7.

1) Hardware

Who doesn't know Moore's law?
I once was asked to produce a benchmark for a Toolkit. After writing and running the code, I gave my results with as conclusion for better results run it on better hardware and they did.

No comments: