java什么时候比c++快(或者JIT什么时候比预编译快)?

when is java faster than c++ (or when is JIT faster then precompiled)?(java什么时候比c++快(或者JIT什么时候比预编译快)?)

本文介绍了java什么时候比c++快(或者JIT什么时候比预编译快)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
JIT 编译器与离线编译器

我听说在某些情况下,由于 JIT 优化,Java 程序或更确切地说是部分 Java 程序能够比 C++ 中的相同"代码(或其他预编译代码)更快地执行.这是因为编译器能够确定一些变量的范围,避免一些条件并在运行时提取类似的技巧.

I have heard that under certain circumstances, Java programs or rather parts of java programs are able to be executed faster than the "same" code in C++ (or other precompiled code) due to JIT optimizations. This is due to the compiler being able to determine the scope of some variables, avoid some conditionals and pull similar tricks at runtime.

你能否举一个(或更好的——一些)例子,这适用于什么地方?并且也许概述了编译器能够优化字节码超出预编译代码可能的确切条件?

Could you give an (or better - some) example, where this applies? And maybe outline the exact conditions under which the compiler is able to optimize the bytecode beyond what is possible with precompiled code?

注意:这个问题不是关于比较 Java 和 C++.它关于 JIT 编译的可能性.请不要着火.我也不知道有任何重复.如果有请指出来.

NOTE : This question is not about comparing Java to C++. Its about the possibilities of JIT compiling. Please no flaming. I am also not aware of any duplicates. Please point them out if you are.

推荐答案

在实践中,您可能会发现在这些情况下,您编写的幼稚 Java 代码胜过幼稚编写的 C++ 代码(所有这些我都亲自观察过):

In practice, you're likely to find your naively written Java code outperform your naively written C++ code in these situations (all of which I've personally observed):

  • 大量的小内存分配/释放.主要的 JVM 都有非常高效的内存子系统,垃圾收集比要求显式释放更有效(如果它真的想的话,它可以移动内存地址等).

  • Lots of little memory allocations/deallocations. The major JVMs have extremely efficient memory subsystems, and garbage collection can be more efficient than requiring explicit freeing (plus it can shift memory addresses and such if it really wants to).

通过方法调用的深层层次结构进行高效访问.JVM 非常擅长删除任何不必要的东西,根据我的经验,通常比大多数 C++ 编译器(包括 gcc 和 icc)要好.部分原因是它可以在运行时进行动态分析(即,它可以过度优化,只有在检测到问题时才去优化).

Efficient access through deep hierarchies of method calls. The JVM is very good at eliding anything that is not necessary, usually better in my experience than most C++ compilers (including gcc and icc). In part this is because it can do dynamic analysis at runtime (i.e. it can overoptimize and only deoptimize if it detects a problem).

将功能封装到短暂的小对象中.

Encapsulation of functionality into small short-lived objects.

在每种情况下,如果您付出努力,C++ 可以做得更好(在空闲列表和块分配/解除分配的内存之间,C++ 几乎在所有特定情况下都可以击败 JVM 内存系统;通过额外的代码、模板和聪明的宏,您可以非常有效地折叠调用堆栈;并且您可以在 C++ 中拥有小型的部分初始化堆栈分配的对象,其性能优于 JVM 的短期对象模型).但你可能不想付出努力.

In each case, if you put the effort in, C++ can do better (between free lists and block-allocated/deallocated memory, C++ can beat the JVM memory system in almost every specific case; with extra code, templates, and clever macros, you can collapse call stacks very effectively; and you can have small partially-initialized stack-allocated objects in C++ that outperform the JVM's short-lived object model). But you probably don't want to put the effort in.

这篇关于java什么时候比c++快(或者JIT什么时候比预编译快)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:java什么时候比c++快(或者JIT什么时候比预编译快)?

基础教程推荐