java 如何编写高效的Java代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/943883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write efficient Java code?
提问by dilig0
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?
众所周知,Java 代码是由 JVM 编译和解释的。我的问题涉及优化:它是仅在运行时由 JVM 优化还是在编译时优化?
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?
为了编写高效的代码,在哪里可以找到支持的优化列表?或者 JVM 优化是否足够强大,以至于我只需要编写可读且易于维护的代码,而不管速度性能如何?
回答by Jon Skeet
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.)
大多数优化是由 JVM 完成的。通常在 JIT 级别比在编译时有更多的优化空间。(“优化”标志实际上是从 中取出的javac,因为事实证明,某些“优化”实际上会损害现实世界中的性能。)
In general (and this applies to many languages/platforms, not just Java):
一般来说(这适用于许多语言/平台,而不仅仅是 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 architectureperform; 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
- 编写尽可能可读和可维护的代码。
- 制定绩效目标和基准,以便您始终可以衡量绩效
- 努力使您的架构发挥作用;以后很难改变(与改变实现相比)
- 更多地从算法的复杂性方面考虑性能,而不是“让它快 10%”:从 O(n^2) 到 O(n) 的变化(通常)要重要得多。(这在很大程度上取决于现实世界的使用情况——如果你只处理 的小值
n,“理论上更好”的算法很容易由于常数因素而变得更慢。) - 使用分析器确定瓶颈所在
- 当分析器认为值得做时,仅以可读性为代价进行微观优化
- 在这样的优化之后进行测量 - 您实际上可能没有太大影响,在这种情况下回滚
回答by omerkudat
The Java HotSpot JIT compiler can detect "hotspots" and adaptively modify the executing code so it delivers better perfomance. Read about it here.
Java HotSpot JIT 编译器可以检测“热点”并自适应地修改执行代码,从而提供更好的性能。在这里阅读。
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.
另一方面,如果您想编写高效的代码,请阅读诸如Robert Simmons 的“ Hardcore Java”或Brian Goetz 的“ Java Concurrency in Practice”之类的书。
回答by Darren Greaves
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
过早的优化通常被视为一件坏事。 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.
当然,使用分析工具测量和证明瓶颈完全是另一回事。如果您这样做并且可以证明存在需要优化的领域,那么您就可以衡量收益并继续优化。
回答by Michael Borgwardt
Is it optimized at run-time by the JVM only or also at compile-time ?
它是仅在运行时由 JVM 优化还是也在编译时优化?
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.
Java 编译器通常很少进行优化(除了解析复合文字),因为“优化”的字节码可能会阻碍 JIT 编译器的优化能力——这才是真正重要的地方。
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?
或者 JVM 优化是否足够强大,以至于我只需要编写可读且易于维护的代码,而不管速度性能如何?
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.
这不是一个相信 JVM 比你优化得更好的问题(尽管这绝对是一个因素),这是一个优化在 95% 的情况下完全无关的问题,因为代码不经常执行。如果一段代码占应用程序执行时间的 0.1%,那根本就不值得费心。即使你可以将它加速 100 倍,它也不会给你带来任何好处。这是最常见的情况。
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.
只要您避免公然愚蠢的事情,您就应该忘记优化,直到您遇到具体的性能问题,然后才准确地优化分析器告诉您是代码中的热点的代码段。
回答by Zef Hemel
Although these tips are meant for Google Android's specific Java implementation, I guess these Google Android performance tipsapply in the "normal" JVM as well. Note that Android's Java does not optimize code at runtime as far as I'm aware.
虽然这些技巧是针对 Google Android 的特定 Java 实现的,但我猜这些 Google Android 性能技巧也适用于“普通”JVM。请注意,据我所知,Android 的 Java 不会在运行时优化代码。
回答by J-16 SDiZ
Don't worry about specific JVM optimization. Those details change from version to version.
不用担心特定的 JVM 优化。这些细节因版本而异。
Buy the "Effective Java" book from Josh Bloch -- this is the best book on this topic.
从 Josh Bloch 那里购买“Effective Java”一书——这是关于这个主题的最好的书。
回答by Hassan Hameed
JVM optimization changes from version to version.
JVM 优化因版本而异。

