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

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

问题描述

javac 一个有趣的-O选项:

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

此选项似乎不受欢迎(隐藏?),我今天才在上发现它. CodeCup 2014页面.

官方中未提及

-O文档 man javac ...很奇怪.

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

Java中的优化主要由JIT编译器在运行时完成. 因此,没有必要试图指示它优化某个 编译时的方式(无论如何仅创建字节码).这 准时,JIT几乎肯定会在现场做出更好的决策 确切的环境并观察执行的实际模式 您代码的特定部分.

我的问题是:

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

也许班级人数可以增加太多,以至于整体表现会下降?还是由JVM进行内联,所以最好还是保留它?

类似的故事 带有gcc -O3标志.

解决方案

根据has an interesting -O option:

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

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

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

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

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.

My question is:

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?

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?

Similar story was with gcc -O3 flag.

解决方案

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

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天全站免登陆