JAVA vs C++之速度—
让我们来看看JIT/Hotspot的工作
一般来说,JVM或JAVA的标准API没有提供让我们观察Hotspot工作(产生机器码)的接口.所以以前我们只能猜测Hotspot在背后究竟做了那些事情,我们写的JAVA代码被它弄成什么样子了.
不过现在好了,Java SE Update N这一系列因为处于开发状态,为了方便debug,这些JVM提供了一个运行参数:PrintOptoAssembly,你可以通过如下方式:
引用
java -XX:+PrintOptoAssembly -server -cp . Main
运行你的程序,就可以看到这些JAVA程序在编译为机器语言之后的样子.
Kohsuke Kawaguchi做了一些测试,他在博客深入JAVA生成的机器码(Deep dive into assembly code from Java)中进行了一些展示与讲解.并且他对JVM生成的优化代码表示赞赏:
引用
All in all, modern JVMs seem pretty good at generating optimal code. In various situations, the resulting assembly code is far from the straight-forward instruction-by-instruction translation.
JAVA之父James Gosling在他的博客Hotspot performance中描述了JIT/Hotspot生成的机器码有以下优点:
引用
◇ Really aggressive inlining
◇ Even storage allocation & initialization gets inlined - new T() really is as efficient as C's alloca() (and it beats the pants off malloc())
◇ Careful management of cache prefetching
◇ Deep understanding of variablity between the flavors of x86 machines
◇ Loop unrolling with warmup/cooldown
◇ "theorum proving away" of array index checks (and many other things)
◇ much cleverness with locks

浙公网安备 33010602011771号