One of my favourite coding activities is profiling – taking a Java application and making it run faster. Every so often we set aside some time to just focus on making our code run faster.
Don’t optimise code where speed does not matter. Not only does it give no benefit, it probably makes that code harder to support and may introduce bugs.
Before you can do this, you need to find the bottlenecks – which bits of the code are used most. These are the sections that are worth improving. You can find these using a Profiler.
In the Java world, we are lucky to have lots of profiling tools. The two we use are the built-in NetBeans and JProfiler. Here is what is JProfiler shows…
Methods that are frequently called or used often are worth looking at. Other methods are not worth bothering with. If you take a method that takes 2% of the time and makes it twice as fast, the code will be a petty 1% faster. If you take a routine used 30% of the time and make it 10% faster (a much easier task) you will get 3 times the benefit.
Profiling also tells you where it might be worth caching values. If you can reduce calls to some routines it will make it faster without having to change code.
You also find some surprises with Java. In one routine, we generated some data and wrote it to a ByteArrayOutputStream. It actually turned out to be 5 times faster if we called it twice instead, first to see how many bytes are created and then again with a byte array to store the data in…
And if you want some more speed in your PDF Viewer, learn more in release notes.