Java collections faster than c++ containers?

后端 未结 5 876
鱼传尺愫
鱼传尺愫 2021-01-31 09:07

I was reading the comments on this answer and I saw this quote.

Object instantiation and object-oriented features are blazing fast to use (faster than C

5条回答
  •  粉色の甜心
    2021-01-31 09:58

    While I am a fan of Java, it is worth noting that C++ supports allocation of objects on the stack which is faster than heap allocation.

    If you use C++ efficiently with all it various ways of doing the same thing, it will be faster than Java (even if it takes you longer to find that optimal combination)

    If you program in C++ as you would in Java, e.g. everything on the heap, all methods virtual, have lots of runtime checks which don't do anything and can be optimised away dynamically, it will be slower. Java has optimised these things further as these a) are the only thing Java does, b) can be optimised dynamically more efficiently, c) Java has less features and side effects so it is easier for optimiser for get decent speeds.

提交回复
热议问题