我应该使用 javac -O 选项来优化吗? [英] Should I use javac -O option to optimize?

查看:29
本文介绍了我应该使用 javac -O 选项来优化吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

javac 一个有趣的-O选项:

通过内联静态、最终和私有方法优化编译代码.请注意,您的类可能会变大.

Optimizes compiled code by inlining static, final and private methods. Note that your classes may get larger in size.

这个选项似乎不流行(隐藏?),我今天才发现,在 CodeCup 2014 页面.

This option seems to not be popular (hidden?), I've just discovered it today, on CodeCup 2014 page.

-O 未在 官方文档 也不在 man javac...奇怪.

-O is not mentioned in the official documentation nor in man javac... Strange.

在对类似问题的公认回答中,我们可以读到:

In accepted answer to similar question, we can read that:

Java 中的优化主要由 JIT 编译器在运行时完成.因此,尝试指示它优化某个特定值是没有意义的编译时的方式(当它只创建字节码时).这JIT 几乎肯定会在现场做出更好的决策,因为知道精确的环境并观察实际的执行模式代码的特定部分.

Optimization in Java is mostly done by the JIT compiler at runtime. Hence there is no point trying to instruct it to optimize a certain way at compile time (when it is creating only bytecode anyway). The JIT will almost surely make better decisions on the spot, knowing the exact environment and observing the actual patterns of execution of specific parts of your code.

我的问题是:

我应该总是使用 -O 选项吗? 换句话说,代码总是使用 -O 运行得更快,或者是这样根本没有区别?

Should I always use the -O option or not? In other words, the code always run faster with -O or does it make no difference at all?

也许类的大小可以增加太多以至于整体性能会下降?或者 JVM 无论如何都会进行内联,所以最好将其保留?

Maybe classes size can increase so much that the overall performance will drop? Or JVM will do the inlining anyway so it's better to leave it to that?

类似的故事gcc -O3 标志.

推荐答案

根据 源代码在第 553 行附近.

It is a no-op according to a comment in the source code around line 553.

当 JIT 编译器效率不高或根本没有 JIT 编译器时,它可能很有用.

It was probably useful when the JIT compiler was not efficient yet or when there was no JIT compiler at all.

这篇关于我应该使用 javac -O 选项来优化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆