[Posts] the Java and C / C ++ compiler Comparison

Java and C / C ++ compiler comparison

https://www.jianshu.com/p/5ca22eaa9c8e

 


0.152017.02.05 13:13:17 read 912 words 1,727
 

Foreword

This two-day review of the Zhou Zhiming "in-depth understanding of the Java Virtual Machine," 2nd edition, found in Chapter 11 Section 4 on the contents of the Java compiler written fantastic - full article is taken from "in-depth understanding of the Java Virtual Machine" on page 2 version 0_o

Java and C / C ++ compiler

Most programmers think that C / C ++ will be faster than the Java language, the Java language even think since the birth of the "slow pace of implementation" of the hat should be deducted at its head, it is due to appear this view Java刚出现的时候即时编译技术还不成熟,主要靠解释器执行的Java语言性能确实比较低下. But the time compilation has been very mature, Java language is possible in terms of speed and C / C ++ to compete against you? To know the answer to this question, let us start from both the compiler.

Java compared with C / C ++ compiler, in fact, it represents the most classic 即时编译器and 静态编译器contrast, to a large extent determine the comparative results of the performance of Java and C / C ++, because both the C / C ++ or Java code, compile final after being executed machines are native machine code, higher performance which language, in addition to their own API库实现地好坏, the rest of the comparison has become a 拼编译器game. Of course, this comparison also excludes one-sided comparison of the efficiency of development, language is better, who fast who slow problems are the result of difficult debate, here we come back to the topic, take a look at compile these two languages What are the advantages of each device.

Java compiler "inferior" reasons

Compared the Java virtual machine in-time compiler and C / C ++ optimizing compiler static, may be due to the following reasons, which led to output native code there are some disadvantages (listed below also includes a number of performance disadvantages virtual machine execution subsystem) :

First, because the run-time compiler is occupied 用户程序的运行时间with a lot of time pressure, it can provide a means to optimize also seriously constrained by the cost of compilation. If the compilation speed below standard, that user will perceive a certain part of the startup program or program to a significant delay, this time compiler hesitant to make large-scale introduction of technology optimization, and cost in a static compile time optimizing compiler not the primary concern.

Second, Java language 动态的类型安全语言, which means that by the virtual machine to ensure that the program does not violate the language semantics or unstructured memory access. From the implementation level point of view, this means that the virtual machine must 频繁地进行动态检查, as an instance method access check null pointer, an array element access range bounds checking, type conversion when checking inheritance relations. For checking the behavior of this type of program code written is not clear, although the compiler will try to optimize, but on the whole still consumes a lot of running time.

Third, Java language, although there is no virtual keyword, but 使用虚方法的频率却远远大于C/C++语言the frequency, which means that runtime polymorphism method recipients were selected much larger than C / C ++ language, also means that after some time compiler optimization (such as the difficulty of method inlining) is much larger than C / C ++ static optimizing compiler.

Fourth, Java language is 动态扩展the language, load a new class runtime possible 改变程序类型的继承关系, which makes it a lot 全局的优化难以进行, because the compiler can not see the whole picture of the program, a number of global optimization can only be optimized in a radical way to accomplish the compiler may not do not always pay attention to the type of change and with the withdrawal or re-optimizations at runtime.

Fifth, Java language, 对象内存是在堆上local variables, the only method to allocate on the stack, and C / C ++ object there are multiple memory allocation, both may be allocated on the heap, they may allocated on the stack, the stack if you can thread private allocation of objects, it will reduce the pressure on memory recovery. In addition, memory C / C ++ mainly by the user application code to recover allocation, which does not exist process useless objects screening, so the efficiency (only operating efficiency, eliminate development efficiency) than garbage collection Java to be higher .

Java compiler of "advantage"

上面所了一堆Java语言在性能上的劣势,这些都是为了换取「开发效率」上的优势而付出的代价,动态安全、动态扩展、垃圾回收这些“拖后腿”的特性,都为Java语言的开发效率做出了很大贡献。

Moreover, there are many optimization is a Java-time compiler can do, and C / C ++ static optimizing compiler can not do or not do. For example, in C / C ++, the alias analysis (Alias ​​Analysis) the difficulty of going much higher than Java. Java type of security ensures that the code is similar to the following, as long as ClassA and ClassB no inheritance, and that the object objA objB could never be the same object, that will not be the same piece of memory two different aliases.

void foo(ClassA objA, ClassB objB) { objA.x = 123; objB.y = 456; // 只要objB.y不是objA.x的别名,下面就可以保证输出为123 print(objA.x); } 

After determining the objA objB and not the other aliases, many associated with the optimization of data dependencies can be (reordering, variable substitution). Specific to this case, that is, without worrying about objB.y and objA.x point to the same piece of memory, so you can safely determine the print statement objA.x 123.

Java compiler Another bonus is its dynamic nature brings, because the C / C ++ compiler optimization are all done at compile period to 运行期性能监控为基础的优化措施it can not be carried out, such as:

  1. Call frequency prediction: Call Frequency Prediction
  2. Frequency branch prediction: Branch Frequency Prediction
  3. Crop unselected branch: UNtaken Branch Pruning

These are the Java language unique performance advantages.

to sum up

With the development of Java JIT compiler technology, Java's speed is fast enough. Java classes can be dynamically loaded at runtime (can be computed on the fly from the zip packet network, generates other files), C / C ++ can not do this completely. Overall, the dynamic security of Java, dynamic expansion, garbage collection and other features, making the development of high efficiency, and flexible enough; at the same time with the development of compiler technology, the performance disadvantage is gradually reduced.

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/12159527.html