when是java比c ++快(或者是当JIT更快然后预编译)? [英] when is java faster than c++ (or when is JIT faster then precompiled)?

查看:163
本文介绍了when是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).

在每种情况下,如果你付出努力,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.

这篇关于when是java比c ++快(或者是当JIT更快然后预编译)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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