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

前端 未结 13 1690
不知归路
不知归路 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:55

    To me, this question is a bit of a red herring (perhaps not intentional). Its really the wrong question to ask.

    The first questions to ask are these

    1. What is keeping my program slow?
    2. For my new program, what are the key performance design considerations?

    Here are some good 'why' questions

    • Is there too much unnecessary I/O?
    • Is too much memory being used?
    • Is the memory allocator being thrashed (too many allocs, too many fine grained object)
    • Is my program blocked on network I/O for long periods
    • Do have locks in the wrong place

    I suspect that you really need to focus on the Performance aspects of your program (with a capital 'P') instea od the performance (little 'p') aspects first. If you can get to the point where the language is in the way, then you have done a really good job to that point with respect to performance.

    For new code - its important to plan for performance and efficiency up front. I always recomend that performance and efficiency treated just like any other feature (they are features): just like UI bling, or reliability. Of course this will depend on many things - but when its important you need to plan for it up front:

    • Pick data structures and algorithms appropriate for the data set and expected scaling
    • Multi-thread UI based apps where appropriate (UI thread, background/processing thread)
    • Plan for long network I/O latencies
    • Plan to set goals and measure performance up front - run regression tests regularly
    • Measure memory usage - memory hogs are slow (let the japes begin :) )
    • Don't poll when there are events, callbacks or other notification mechanisms

    The reason I think this is a red herring is that rarely does one just get to choose between C++ and Java - they are very, very diffrent languages with very different run times. I suspect is is more usual that you have other constraints pushing you one way or other - these will be higher order factors than language performance. Computability with existing code, skills and experience of the exiting staff, etc. etc.

    The environment makes a difference too. For example, Java would almost never be the right choice for a widows client (vs. a web) application. Conversely, native C++ would almost never be the choice for a web based app. (note, I am a windows guy - the situation may be very diffretnt in *nix).

提交回复
热议问题