Why does Java have much better performance and other interpreted languages?

Why does Java have much better performance than other interpreted languages ​​like Python? I know this is probably due to the fact that it was compiled in advance, but what about concurrency?

How is the JVM capable of working much better with parallel programs, while interpreted languages ​​must deal with things like locking the global interpreter, etc., which really slows things down?

+8
java performance jvm interpreter
source share
2 answers

This is a really interesting question, but I'm not sure there is an easy way to answer it. JVMs currently use a number of high-performance optimizations to improve performance. Here are a few:

  • Dynamic compilation:. The best JVMs can dynamically compile bytecode directly into machine code, which is then executed at native speeds.
  • Polymorphic inline caching:. Many JVMs use built-in caching to try to improve the performance of method dispatch, remembering which functions were called in the past.
  • Static typing:. Because Java is statically typed, bytecode instructions rarely require expensive introspection on the type of an object to determine how to perform an operation on it. Field offsets can be calculated statically, and indexes of methods in the virtual function table can also be pre-calculated. Contrast this with languages ​​like JavaScript, which have no static input and are much harder to interpret.
  • Garbage collection: The JVM garbage collector is optimized for efficiently distributing and freeing objects. It uses a combination of mark-and-sweep and stop-and-copy labels to make most allocations very fast and make it quick to recover large amounts of memory.
  • Known bottlenecks:. Instead of having a huge VM lock, some JVM implementations automatically insert additional code into each part of the compiled / interpreted code to periodically register with the virtual machine and determine if they can support running. Thus, if the JVM needs to do garbage collection in only a few threads, it can do this while other threads are running. If it is necessary for the stop-peace operation, this will happen only when the threads hit certain points, which means that simple operations do not need to constantly check the state of the virtual machine.

There are many, many optimizations that I probably don’t know, but I hope this helps you get the answer!

+11
source share

During compilation, Java code has no optimization.

The JIT runtime does most of the compilation.

What could be different in Java is that it works relatively poorly with minimal side effects. This simplifies code optimization.

while interpreted languages ​​have to deal with things like locking the global interpreter, etc., what really slows things down?

This is a problem with implementation. Java has been designed to support multithreading from the start. I suspect that python was designed for scripts and fast development cycles, which makes it much better.

+3
source share

All Articles