是否有可能比C中相同的程序(优化的)更快地获得Java程序? [英] Is it possible to get a Java program faster than the same program (optimized) in C?

查看:57
本文介绍了是否有可能比C中相同的程序(优化的)更快地获得Java程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于在编译时产生C/C ++优化,而在运行时产生Java优化这一事实.是否有可能使Java程序比C中经过优化的同一程序更快?

Regarding the fact that C/C++ optimization are produced at compilation time, and Java optimizations are produced at runtime. Is it possible to get a Java program faster than the same program (optimized) in C?

我知道运行时优化可能比编译时间要好.因此,我想知道是否可以将这些优化的收益与运行JVM的开销进行比较.

I understand that runtime optimizations can be better than compilation time. Hence I am wondering if the gain of these optimization can be compared to the overhead of running the JVM.

推荐答案

从理论上讲,是的. 实际上,这种可能性很小.

In theory, yes. In practice, it is highly unlikely.

一个基本假设是C/C ++为二进制操作码目标编译一次,而Java是为其运行的特定机器编译的.这应该使Java更具优势.但是现实是,即使C/C ++也可以有多个优化路径,这些优化路径可以在运行时动态选择,并可以从特定目标编译中获得大部分好处.

One of the fundamental assumption is that C/C++ is compiled once for a binary opcode target, and that Java is compiled for the specific machine it is running on. This should give an edge to Java. But the reality is that even C/C++ can have several optimization path, dynamically selected at run time, and get most of the benefit of specific-target compilation.

相反,如Rekin所述,Java JVM需要动态地剖析Java程序,以了解要优化的内容和方法.分析本身就是一项昂贵的操作,并且不能从Java JVM中消除开销.另一方面,为当前工作负载选择正确的优化集可以带来优势.实际上,大多数C程序(但不是全部:)都针对其任务进行了很好的调整,并且几乎没有使用概要分析技术进行优化的地方.

Conversely, as stated by Rekin, the Java JVM needs to dynamically profile the Java program to know what to optimize and how. Profiling is an expensive operation in itself, and that overhead can't be taken off Java JVM. On the other hand, selecting the right set of optimization for the current workload can give an edge. In practice, most C programs (but not all :) are well tuned for their task, and there is little left to optimize using profiling techniques.

Java中还有其他影响完全使这些编译问题相形见effects.可能排在第一位的是垃圾收集器.

There are other effects in Java which completely dwarf these compilation issues. Probably at first position is the Garbage Collector.

垃圾收集器的首要任务是简化编程,处理并避免C/C ++最讨厌的经常性错误之一,即内存泄漏.仅此功能就可以证明Java在许多工业环境中的广泛使用.

Garbage collector first mission is to ease programming, take care of, and avoiding one of the most nasty recurrent bug of C/C++, memory leaks. This feature alone justify the large use of Java in many industrial environment.

但是,这是有成本的.一个很大的.根据研究,有必要提供大约5倍的必需内存,以确保垃圾收集器以最小的开销工作.因此,每当缺少如此大的内存时,GC开销就会开始变得可观,从而使性能下降.

However, it has a cost. A very large one. According to studies, it is necessary to have available about 5x the amount of strictly necessary memory to make sure garbage collector works with minimal overhead. So, whenever such amount of memory is lacking, GC overhead starts to become significant, putting performance to a crawl.

相反,在某些情况下,释放内存分配费用算法可能会允许更改算法,并采用更好,更快的算法.在这种情况下,Java可以获得优势,并且比C程序要快.

Adversely, it may happen, in some circumstances, that freeing the algorithm of memory allocation charge may allow to change the algorithm, and adopt a better, faster one. In such a circumstance, Java can get the edge, and be faster than a C program.

但是您可以猜到,这并不常见...

But as you can guess, this is uncommon...

这篇关于是否有可能比C中相同的程序(优化的)更快地获得Java程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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