我应该看看由java编译器产生的字节码吗? [英] Should I look at the bytecode that is produce by a java compiler?

查看:200
本文介绍了我应该看看由java编译器产生的字节码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有


  • JIT编译器可以将字节码完全不同。

  • 这将导致您进行过早优化。


  • 您不知道JIT将编译哪个方法,因此,

我没有真的要求

推荐答案

是的,但在一定程度上,它是好的一个教育机会,看看发生了什么,但可能应该做的适度。

Yes, but to a certain extent -- it's good as an educational opportunity to see what is going on under the hood, but probably should be done in moderation.

这可能是一件好事,因为看看字节码可能会帮助了解如何将Java源代码编译成Java字节码。此外,它可以给出一些关于编译器将执行什么样的优化的想法,并且可能对编译器可以执行的优化的数量有一些限制。

It can be a good thing, as looking at the bytecode may help in understanding how the Java source code will be compiled into Java bytecode. Also, it may give some ideas about what kind of optimizations will be performed by the compiler, and perhaps some limitations to the amount of optimization the compiler can perform.

例如,如果执行字符串连接, javac 将优化连接到使用 StringBuilder 并执行 append 方法以串联 String

For example, if a string concatenation is performed, javac will optimize the concatenation into using a StringBuilder and performing append methods to concatenate the Strings.

但是,如果字符串连接在一个循环中执行,每次迭代可以实例化一个新的 StringBuilder ,导致与手动实例化 StringBuilder 在循环之外,并且只在循环内执行 append

However, if the string concatenation is performed in a loop, a new StringBuilder may be instantiated on each iteration, leading to possible performance degradation compared to manually instantiating a StringBuilder outside the loop and only performing appends inside the loop.

关于JIT的问题。准时编译将是JVM实现特定的,所以不容易找出当字节码被转换为本地代码时实际发生的事情,此外,我们不能知道哪些部分正在JITted(至少没有一些JVM特定的工具来看看正在执行什么样的JIT编译 - 我不知道这方面的任何细节,所以我只是猜测。)

On the issue of the JIT. The just-in-time compilation is going to be JVM implementation specific, so it's not very easy to find out what is actually happening to the bytecode when it is being converted to the native code, and furthermore, we can't tell which parts are being JITted (at least not without some JVM-specific tools to see what kind of JIT compilation is being performed -- I don't know any specifics in this area, so I am just speculating.)

也就是说,JVM将要执行字节码,它被执行的方式或多或少对于开发人员不透明,并且也是特定于JVM的。可能有一些JVM执行的性能技巧,而另一个没有。

That said, the JVM is going to execute the bytecode anyway, the way it is being executed is more or less opaque to the developer, and again, JVM-specific. There may be some performance tricks that one JVM performs while another doesn't.

当涉及到查看生成的字节码的问题时,它归结为学习当源代码被编译为字节码时实际发生了什么。能够看到编译器执行的各种优化,但也了解编译器可以执行优化的方式有限制。

When it comes down to the issue of looking at the bytecode generated, it comes down to learning what is actually happening to the source code when it is compiled to bytecode. Being able to see the kinds of optimizations performed by the compiler, but also understanding that there are limits to the way the compiler can perform optimizations.

这么说,我认为这是一个很好的主意,成为对字节码生成的痴迷,并试图编写将发出最优化的字节码的程序。更重要的是编写其他人可读和可维护的Java源代码。

All that said, I don't think it's a really good idea to become obsessive about the bytecode generation and trying to write programs that will emit the most optimized bytecode. What's more important is to write Java source code that is readable and maintainable by others.

这篇关于我应该看看由java编译器产生的字节码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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