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?
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.
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):
n
, the "theoretically better" algorithm can easily end up being slower due to constant factors.)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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With