Java Performance tuning

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…

Profiler

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…

So if you have not tried profiling your code, try NetBeans or the JProfiler demo on some code and be amazed at what is going on.

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!

Related Posts:

The following two tabs change content below.

Mark Stephens

System Architect and Lead Developer at IDRSolutions
Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.
Markee174

About Mark Stephens

Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX.

He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>