Can anyone quantify performance differences between C++ and Java?

前端 未结 13 1660
不知归路
不知归路 2021-01-31 11:33

Java was initially slow before the JIT but today performance is pretty close to C++. I want to know if someone has done measurable performance comparisons between the two langu

13条回答
  •  盖世英雄少女心
    2021-01-31 11:51

    Where does Java fall short when compared to C++?

    Excellent question. Java and the JVM have two main deficiencies that cripple performance compared to C++:

    • Generics based upon type erasure.

    • Lack of value types.

    The former means that generic code incurs boxing and unboxing which incur massive amounts of unnecessary allocation and extra levels of indirection that are cache unfriendly.

    The latter means it is impossible for the programmer to unbox arbitrary data structures like complex numbers (pairs of floats), hash table entries (key-value pairs) and vertex data.

    Both of these problems combine to make it impossible to implement an efficient generic hash table in Java. In particular, .NET solved both of these problems. For example, Java's generic hash table can be 17× slower than a .NET Dictionary.

    In addition, JVMs have very slow FFIs compared to C++. I've heard that simply invoking an external C function from Java can take 1,000 cycles.

提交回复
热议问题