Why C++ is slower than JAVA or Python ? That's unbelievable !


  • 0
    C

    The fastest C++ code can achieve a time about 370ms ~ 380ms

    while Python code can achieve 100ms, and even amazing, JAVA can do it in just 4ms !

    This result is quite counterintuitive.


  • 0
    V

    I think the C++ version needs to allocate every node in the system heap, which is a horribly time-consuming operation.

    The Python and Java version probably have some kind of memory pool.


  • 2

    Java currently only measures the runtime in the user function execution excluding the JVM startup time plus the driver code execution time. C++ measures the entire program execution, so it is not meaningful to compare the runtime between languages.


  • 0
    C

    I get it, thanks for your answer!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.