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

前端 未结 13 1667
不知归路
不知归路 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条回答
  •  梦毁少年i
    2021-01-31 11:39

    To complete Pax's and Uri's answer, here are some recent benchmarks:

    • Performance Comparison - C++ / Java / Python / Ruby/ Jython / JRuby / Groovy
    • Java Performance (wikipedia)

    As said, those are two very different languages, and some are convinced that Java will ever be slower than C++ because of:

    • Heap allocation for all objects (even small ones like iterators)
    • lots of dynamic castings
    • increased memory usage

    [Humor]

    "Java is high performance. By high performance we mean adequate. By adequate we mean slow." Mr. Bunny

    As mentioned by dribeas in the comments, Heap allocation is not a good argument.
    This "Urban performance legends, revisited" mentions:

    "Garbage collection will never be as efficient as direct memory management." And, in a way, those statements are right -- dynamic memory management is not as fast -- it's often considerably faster.
    The malloc/free approach deals with blocks of memory one at a time, whereas the garbage collection approach tends to deal with memory management in large batches, yielding more opportunities for optimization (at the cost of some loss in predictability).

提交回复
热议问题