What standard optimization refactoring can I do for my Java application?

I have a semi-large Java application. It is written quite poorly, and I suspect there are quite a few simple things that I can do that will fix things a bit and improve performance.

For example, I recently found

String.matches(regex) 
Function

used quite a lot in loops with the same regular expression. So I replaced this with precompiled templates . I use FindBugs (which distinguishes BTW), but this did not reveal this problem, and I am limited to the tools that I can use here at work.

Is there anything else like this that I should look at?

+4
source share
6 answers

Take a look at these various tools:

I also suggest looking at Sonar , which is a great shell for all of these tools.

All of these tools are free!

Globally, they will not help you improve performance, but the quality of the code.

+5
source

First of all, make it a well-written application. In my experience, most of the performance benefits come not from stupid deeds, but not from smart optimizations.

When you have a well-written application, then it's time to start the profiler and optimize only what matters.

+5
source

Is performance a problem? If this is the case, I would not redo anything until I profiled the code in a variety of conditions and did not have hard data to tell me where the time was spent. Perhaps you are changing things that are of no use.

I will worry about thread safety. How much attention was paid to this?

If you intend to reorganize, start by writing JUnit tests first. This will help you familiarize yourself with the code and provide you with a security system. Tests must pass before and after your changes.

Most importantly, don't do a lot of refactoring just because you think it's a mess. Your team and client should be on board with what you do before you begin. Have you shared your (admittedly good) ideas with others? Software is a team sport; communication is the key.

+4
source

One very important thing to do when refactoring to improve an application is to first reorganize the code so that the source code looks at least decent. After that, never guess where to optimize, always measure the bottlenecks and concentrate on solving these problems. As a rule, programmers like to do such things as exchanging recursive methods for loops, choosing the exact right sorting algorithm, etc., which very often differs very little. So be sure to focus on the correct area (using too much CPU? Memory? Too many threads? Long thread locks?)

EDIT: Like any other poster, of course, make sure that other members of your team / your boss consider this work a decent job, if this is not a problem for them, they are likely to take less care.

+1
source

Run the code through the profiler (check speed and memory). When you find where it is slow (usually not where you think it is), find out what you can do to speed it up.

Another useful thing (if you are a little brave) is to use NetBeans 7.0 M2 (don’t worry too much, their non-releasing version is very stable) there is a plugin called β€œJackpot” that searches for your code for refactoring. Some of them are related to performance ... but I do not think that any of them will make a radical change in speed.

Generally speaking, keep the code clean and easy to read, and it will be fast. When it's not fast, it will be easier for you to speed it up than if it were a mess.

What I did once when I was writing something that I knew was fast (it was code for parsing class files) was to start the profiler every time I made changes. So in one step, I decided to shorten memroy by calling String., An intern, to make sure all the lines were merged together. When I added the call to intern (), memry dropped a little, but the time increased by some huge amount (String.intern is too slow, or it was a few years ago). So, at that moment I knew that what I just did was inefficiently slow, and I canceled this change.

I do not recommend doing this in general development, but I run the code through the profiler once a day or once a week to see how this happens - this is not a performance killer.

0
source

If you are a book reader, I would highly recommend Josh Bloch Effective Java .

0
source

All Articles