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 which 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 one 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 which takes 2% of the time and make 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, try our new 4.21 release here.
This post is part of our “Java Articles Index” series. In these articles, we aim to explore the world of Java and Javafx. Have a look through!
Latest posts by Mark Stephens (see all)
- My experience of a Turkish bath (visiting Istanbul for DevFest) - November 24, 2017
- My 5 key takeaways from JavaOne 2017 - October 6, 2017
- My notes and pictures from thursday JavaOne 2017 - October 5, 2017
- My notes and pictures from Wednesday JavaOne 2017 - October 5, 2017
- My notes and pictures from Tuesday JavaOne 2017 - October 4, 2017