4

I want to optimize my Java source code, but I am not sure what parts of my code may be optimized. Is there any tool or any technique through which I can optimize my Java source code?

I know the basic fundamentals of code optimization, but I have a large number of lines and very complex source code (Java classes).

Can I optimize my code with the help of a Java profiler?

Thanks in Advance!

Adam Paynter
  • 46,244
  • 33
  • 149
  • 164
Ravi Parmar
  • 1,392
  • 5
  • 24
  • 46
  • 2
    It is so refreshing to read a question about optimization which involves the use of *measurement* to determine which code needs optimizing! – Adam Paynter Feb 14 '11 at 11:59
  • @Adam .. Thanks. But what you mean to say ...CONFUSED ::-( ? – Ravi Parmar Feb 14 '11 at 12:07
  • 2
    This isn't a full answer, but: **remember your goal** throughout the process. Before you start, think about *exactly why* you're optimising (i.e. what are your target throughput rates and how are you measuring them), then identify areas of code that are responsible for this target being missed at present. If you don't have a particular goal in mind, it's unlikely you should optimise "just because". *Fast enough* is fast enough. – Andrzej Doyle Feb 14 '11 at 12:09
  • @water: Many people *think* they know which parts of their program needs optimizing (without using any measurement tools). Unfortunately, it is very difficult to predict which parts need optimizing without the use of measurement tools. Some people try to optimize the parts of the program that didn't actually need optimizing. When that happens, their work is wasted. – Adam Paynter Feb 14 '11 at 12:15
  • 1
    @water: Can you accept the idea that it's actually very easy to do? [Here's the method in Java.](http://stackoverflow.com/questions/266373/one-could-use-a-profiler-but-why-not-just-halt-the-program/317160#317160) The advice that you should not guess, and you should not micro-optimize without profiling (which is a form of guessing), are exactly right. However, for finding what to optimize, many people say "measure measure", but that's actually pretty indirect. Try the above method. It's crude but very effective. – Mike Dunlavey Feb 14 '11 at 14:20

3 Answers3

3

What do you mean by optimising your code? DO you want to refactor your code to simplify it. In that case I suggest you use your IDE to help refactor the code.

If you want to minimise its memory / CPU consumption, I would suggest you use a profiler like VisualVM or YourKit to idenify where resources are being consumed.

A code analysis tool can also help pick up the obvious performance issues. I have code analysis on as I type in my IDE which helps me pick up those issues as I write it.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 3
    Once you have identified sections of code which need to perform better, you may want to refactor them. Simpler code often runs faster too. ;) The two tend to go together. – Peter Lawrey Feb 14 '11 at 12:04
3

Performance optimization - yes, a profiler may help. At least it can show you those areas in your application that take an unexpected amount of CPU time.

But before starting to apply changes - take care not to do some microoptimization. Look at improving algorithms first. Sometimes we use nested for loops while a task can be done with a single one (linear time). Double check if you use the correct collection types. Then have a look if you accidentally create more objetcs than needed (object creation in loops is a typical reason for performance problems).

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
0

Their are several tools for static code analyzes (to do code style / code convention / best practises / bug "optimization" of your code).

I would recommend using Sonar. It covers them all and is easy to setup - even on a local machine (unzip and start). It is best used with maven projects but also possible for NON maven projects.

FrVaBe
  • 47,963
  • 16
  • 124
  • 157
  • Yes, each tool comes with an eclipse plugin. Sonar is a quality management platform which runs on its own but the analyze results can be also bind to the code with an eclispe plugin). It is definitely worth to try! – FrVaBe Feb 14 '11 at 12:10