为什么Java具有比其他解释语言更好的性能? [英] Why does Java have much better performance vs other interpreted languages?

查看:279
本文介绍了为什么Java具有比其他解释语言更好的性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么与其他解释型语言(如Python)相比,Java的性能要好得多?我知道这可能与事前编译有关,但是并发呢?

Why does Java have much better performance compared to other interpreted languages like Python? I know this probably has something to do with the fact that it's compiled beforehand, but what about concurrency?

JVM如何在并发程序中表现出更好的性能,而解释语言必须处理诸如全局解释器锁定之类的事情,这确实会减慢速度?

How is the JVM able to perform so much better with concurrent programs, whereas interpreted languages have to do deal with things like global interpreter locking etc, that really slow things down?

推荐答案

这是一个非常有趣的问题,但是我不确定是否有简单的答案.如今,JVM使用一系列高度积极的优化来尝试提高性能.这里有一些:

This is a really interesting question, but I'm not sure there's a simple way to answer it. JVMs these days use a range of highly aggressive optimizations to try to improve performance. Here are a few:

  • 动态编译:大多数优秀的JVM可以将字节码直接动态编译为机器代码,然后以本机速度执行.
  • 多态内联缓存:许多JVM使用内联缓存进行尝试通过记住过去调用过的函数来提高方法分派的性能.
  • 静态类型输入:由于Java是静态类型的,因此字节码指令很少需要对对象的类型进行昂贵的自省,以决定如何对该对象执行操作.字段偏移量可以静态计算,虚拟函数表中的方法索引也可以预先计算.与此形成对比的是像JavaScript这样的语言,它们没有静态的类型并且很难解释.
  • 垃圾收集:JVM垃圾收集器经过优化,可以高效地分配和释放对象.它结合了标记清除和停止复制技术,可以真正快速地完成大多数分配,并可以轻松快速地回收大量内存.
  • 已知的难点:某些JVM实现并没有拥有巨大的VM锁定,而是自动将额外的代码插入到每个已编译/解释的代码中,以定期检查VM并确定它们是否可以保留跑步.这样,如果JVM仅需要在几个线程中进行垃圾回收,则可以在其他线程运行时进行垃圾回收.如果需要执行停止操作,则仅在线程达到特定点时才会发生,这意味着简单的操作不必连续检查VM状态.
  • Dynamic compilation: Most good JVMs can dynamically compile the bytecode directly into machine code, which then executes at native speeds.
  • Polymorphic inline caching: Many JVMs use inline caching to try to improve the performance of method dispatching by remembering which functions have been called in the past.
  • Static typing: Since Java is statically-typed, bytecode instructions rarely have to do expensive introspection on the type of an object to determine how to perform an operation on it. Field offsets can be computed statically, and method indices in a virtual function table can be precomputed as well. Contrast this with languages like JavaScript, which don't have static typing and are much harder to interpret.
  • Garbage collection: The JVM garbage collector is optimized to allocate and deallocate objects efficiently. It uses a combination of mark-and-sweep and stop-and-copy techniques to make most allocations really fast and to make it easy to reclaim lots of memory quickly.
  • Known choke points: Rather than having a huge VM lock, some JVM implementations automatically insert extra code into each piece of compiled/interpreted code to periodically check in with the VM and determine whether they can keep running. That way, if the JVM needs to do garbage collection in only a few threads, it can do so while the other threads are running. If it needs to do a stop-the-world operation, it will only occur when the threads hit specific points, meaning that simple operations don't have to continuously check in with the VM state.

我可能还不知道还有很多其他优化方法,但是我希望这可以帮助您找到答案!

There are many, many more optimizations in place that I'm probably not aware of, but I hope that this helps you get toward an answer!

这篇关于为什么Java具有比其他解释语言更好的性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆