Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write efficient Java code? [closed]

As all of you may know, Java code is compiled and interpreted by JVMs. My questions deal with optimization: Is it optimized at run-time by the JVM only or also at compile-time?

In order to write efficient code, where can I find the list of supported optimizations? Or are JVM optimizations powerful enough so that I just have to write code that is readable and easy to maintain regardless of speed performance?

like image 372
dilig0 Avatar asked Jun 03 '09 09:06

dilig0


People also ask

How do you end code in Java?

In Java exit() method is in java. exit() method terminates the current JVM running on the system which results in termination of code being executed currently.


4 Answers

Most optimisation is done by the JVM. There's generally more scope for optimisation at the JIT level than at compile-time. (The "optimise" flag was actually taken out of javac, because it turned out that some "optimisations" actually hurt performance in the real world.)

In general (and this applies to many languages/platforms, not just Java):

  • Write code that is as readable and maintainable as possible.
  • Have performance goals and benchmarks so you can always measure performance
  • Put effort into making your architecture perform; it's hard to change that later (compared with changing implementation)
  • Think about performance more in terms of complexity of algorithms than "make it 10% faster": a change from O(n^2) to O(n) is (generally) much more important. (It very much depends on what the real world usage will be though - if you're only going to be dealing with small values of n, the "theoretically better" algorithm can easily end up being slower due to constant factors.)
  • Use a profiler to determine where your bottlenecks are
  • Only micro-optimise at the cost of readability when the profiler suggests it's worth doing
  • Measure after such an optimisation - you may not have actually had much impact, in which case roll back
like image 94
Jon Skeet Avatar answered Oct 26 '22 23:10

Jon Skeet


The Java HotSpot JIT compiler can detect "hotspots" and adaptively modify the executing code so it delivers better perfomance. Read about it here.

On the other hand, if you want to write code which is efficient to begin with, read a book such as "Hardcore Java" by Robert Simmons or "Java Concurrency in Practice" by Brian Goetz.

like image 42
omerkudat Avatar answered Oct 27 '22 00:10

omerkudat


I'd definitely choose writing code for readability and maintainability over supposed optimisations.

Premature optimisation is generally viewed as a bad thing. http://en.wikipedia.org/wiki/Optimization_(computer_science)#When_to_optimize

Of course, measuring and proving bottlenecks with profiling tools is another matter altogether. If you do this and can prove there are areas that need optimising and you can then measure the benefits go ahead and optimise away.

like image 41
Darren Greaves Avatar answered Oct 27 '22 00:10

Darren Greaves


Is it optimized at run-time by the JVM only or also at compile-time ?

Java compilers typically do very little optimization (apart from resolving compund literals), since "optimized" bytecode may impede the ability of the JIT compiler to optimize - and that's where it really matters.

Or are JVM optimizations powerfull enough so that I just have to write code that is readable and easy to maintain regardless of speed performances?

It's not a question of trusting in the JVM to optimize better than you do (though this is definitely a factor), it's a question of optimization being completely irrelevant 95% of the time, since the code is not executed frequently. If a piece of code accounts for 0.1% of your app's execution time, it's simply not worth bothering with. Even if you can speed it up 100 times, it gains you nothing. And this is the most common case.

As long as you avoid blatantly stupid things, you should forget about optimization until you have a concrete performance problem, and then only optimize exactly the pieces of code that a profiler tells you are hot spots in your code.

like image 40
Michael Borgwardt Avatar answered Oct 27 '22 00:10

Michael Borgwardt