Everyone tells you to do performance tuning last. Why?
Because you can make performance tuning empirical. If you've done your tests first you'll be able to change the code to be performant, without breaking it.
The following process works for me as follows:
0) Prioritize and InstrumentRecord how long things take in your app. This can be as simple as paper and pencil, or as sophisticated as automated performance tests suites.
Look at what is slow. Where would you get your biggest bang for the buck? What would make your users the happiest? Where will the load be in your system (back of envelope math works well here)?
Are they having trouble with the GUI, or the calculations taking to long? Which page? Help them.
1) Investigation and Hypothesis
You need to understand what the application is doing and how it does it. Use a profiler such as JProfiler. Eclipse has a free but slow one (tptp).
Look at time spent in methods - can you reduce the time?
Look at who is allocating objects - can you reduce the number of objects?
Look at number of calls to a method - does it make sense?
Hypothesize where you can save time, have rough guess of proportion of time it will save.
Example: Alan and I found a method was called 20x more often than we expected. It was a bug in the code that was doing unnecessary calculations.
2) ExperimentDo actual timings on the code you are running (I use println like utilities). Youcan't use a profiler for this because of dialation effects (e.g. the OS and Database are not being slowed down, only your java code - the profiler may be lying to you).
Do back of envelope calculations to see if what you're doing makes sense.
Calculate: Theoretical maximum you can save, and likely amount you can save.
For example: assume you find a method you can double in speed. Wow that seems like alot of savings. So lets say this method is taking 10 seconds out of a 100 second process.
Now lets assume you speed it up so it takes zero time:
Theoretical maximum: 10/100 = 10%. Will that satisfy your users?
Likely savings (Double speed of method) = 5/100 = 5%. Will your users notice?
3) ConclusionIs it worth doing?
If you know your users need a lot more speed 5% won't cut it, you'll have to go back to investigating and finding where to improve.
Otherwise implement the change and then measure your actual speed gain.
Empirical Performance improvements. I find them worth the wait.