Actors, CC-BY 4.0 by Yannick Kirschhoffer

How to determine the best implementation of a Java method

Short answer

  1. Write alternative versions of the method you want to test. They should be named as the original, and suffixed with an index. The rest of the signature must not change.
  2. Download the java-implementation-comparer project from Github.
  3. Build it to add it to your local Maven repository: mvn install
  4. Create a test project importing both:
    • the project you want to test;
    • thejava-implementation-comparer:
  5. Create a test class and write the little code you need:
  6. Just enjoy your comparison:

Something more detailed

Context

When writing or refactoring code, understanding the basics of the Java virtual machine is often enough to find which way will be the best, though it is also important to keep maintainability in mind.

However, sometimes, you are unsure, or just may wish to tinker. For these cases, I wrote myself a little Java project, which I use when in doubt. The instructions above are just my way of using it, but it is quite straightforward, so you may find your own way.

What it does and does not

Basically, at the moment, the project only compares the running time of the various implementations.

In the future, I wish to add a hint of the memory each method uses. However, I am unconvinced about the precision I can get (delta of consumed before and after an iteration, and request a garbage collection between each, but it is the JVM which will rule the GC in the end, and it may do it inside the tested method…).

You can have a look at the roadmap and suggest new ideas or make any feedback.

What are checks and iterations?

When you want to compare the performance of two methods, it is common practice to execute it several times and calculate the mean execution time. This is what I call iterations in my project.

As to the checks, it is a set of iterations of each method variant.

So, if the number of checks is 3, the number of iterations is 10,000 and there are two variants, the comparer will do the following:

  • First check:
    • Execute hello  10,000 times.
    • Execute hello1  10,000 times.
    • Execute hello2  10,000 times.
  • Redo the first check two more times.

This was an attempt to mitigate potential caching effects of the processor or the JVM, but if you think this is unnecessary, you can just set the check number to 1.

Why is it not on any Maven repository?

I do not think this has its place on the Maven repository. It is barely a development utility and should not be included into any final project.

If you think otherwise though, feel free to signal it in the comments below (or on the issues page). If you are numerous or your arguments are compelling enough, you may sway my opinion.

Need support?

You can use:

Feel free to let me know if the library is useful to you, too. Good optimizing!

Related archive

Image courtesy from Alcibiade (CC-Attribution 4.0)

Published by

Cyrille Chopelet

Programming addict, UX philosopher, casual gamer, sci-fi enthusiast, hi-tech dilettante, ... Some people even call me a geek.

Leave a Reply