Python 3解释器是否具有JIT功能? [英] Does the Python 3 interpreter have a JIT feature?

查看:95
本文介绍了Python 3解释器是否具有JIT功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现,当我向Python提出更多要求时,python不会100%使用我的机器资源,而且速度并不快,与许多其他解释型语言相比,它是很快的,但是与编译语言相比,我认为差异确实非常明显.

I found that when I ask something more to Python, python doesn't use my machine resource at 100% and it's not really fast, it's fast if compared to many other interpreted languages, but when compared to compiled languages i think that the difference is really remarkable.

是否可以使用Python 3中的即时(JIT)编译器来加快处理速度?

Is it possible to speedup things with a Just In Time (JIT) compiler in Python 3?

通常,JIT编译器是唯一可以提高解释语言性能的东西,因此,我指的是这种方法,如果有其他解决方案可用,我希望接受新的答案.

Usually a JIT compiler is the only thing that can improve performances in interpreted languages, so i'm referring to this one, if other solutions are available i would love to accept new answers.

推荐答案

首先,Python 3(.x)是一种语言,它可以有许多实现.好的,到目前为止,除CPython之外,没有其他实现实际实现该语言的那些版本.但这将会改变(PyPy正在追赶).

First off, Python 3(.x) is a language, for which there can be any number of implementations. Okay, to this day no implementation except CPython actually implements those versions of the language. But that will change (PyPy is catching up).

要回答您要问的问题:CPython(3.x或其他版本)不包含,也从未包含,甚至可能也不会包含JIT编译器.其他一些Python实现(本机为PyPy,Jython和IronPython,通过在其构建的虚拟机上重新使用JIT编译器)确实具有JIT编译器.而且,当他们添加Python 3支持时,他们的JIT编译器没有理由停止工作.

To answer the question you meant to ask: CPython, 3.x or otherwise, does not, never did, and likely never will, contain a JIT compiler. Some other Python implementations (PyPy natively, Jython and IronPython by re-using JIT compilers for the virtual machines they build on) do have a JIT compiler. And there is no reason their JIT compilers would stop working when they add Python 3 support.

但是当我在这里时,也让我解决一个误解:

But while I'm here, also let me address a misconception:

通常,只有JIT编译器才能提高解释语言的性能

Usually a JIT compiler is the only thing that can improve performances in interpreted languages

这是不正确的. JIT编译器以其最基本的形式,仅消除了解释器开销,这可以解决您看到的某些速度下降,但不能解决大多数问题.一个 good JIT编译器还执行了许多优化操作,这些优化操作通常消除了实现众多Python功能所需的开销(通过检测允许更有效实现的特殊情况),例如动态类型化,多态性,以及各种内省功能.

This is not correct. A JIT compiler, in its most basic form, merely removes interpreter overhead, which accounts for some of the slow down you see, but not for the majority. A good JIT compiler also performs a host of optimizations which remove the overhead needed to implement numerous Python features in general (by detecting special cases which permit a more efficient implementation), prominent examples being dynamic typing, polymorphism, and various introspective features.

仅实现 a 编译器对此无济于事.您需要非常聪明的优化,其中大多数仅在非常特定的情况下并且在有限的时间范围内有效. JIT编译器在这里很容易,因为它们可以在运行时生成专门的代码(这是它们的重点),可以通过在运行时观察程序来更轻松(更准确)地分析程序,并且可以在无效时撤消优化.与提前编译器不同,它们还可以与解释器进行交互,并且经常这样做是因为这是明智的设计决策.我想这就是为什么他们与口译人员联系在一起的原因,尽管他们可以并且确实可以独立存在.

Just implementing a compiler does not help with that. You need very clever optimizations, most of which are only valid in very specific circumstances and for a limited time window. JIT compilers have it easy here, because they can generate specialized code at run time (it's their whole point), can analyze the program easier (and more accurately) by observing it as it runs, and can undo optimizations when they become invalid. They can also interact with interpreters, unlike ahead of time compilers, and often do it because it's a sensible design decision. I guess this is why they are linked to interpreters in people's minds, although they can and do exist independently.

除了优化解释器的代码本身之外,还有其他方法可以使Python实现更快,例如HotPy(2)项目.但是这些技术目前处于研究或实验阶段,尚未显示出其有效性(和成熟度).真实代码.

There are also other approaches to make Python implementation faster, apart from optimizing the interpreter's code itself - for example, the HotPy (2) project. But those are currently in research or experimentation stage, and are yet to show their effectiveness (and maturity) w.r.t. real code.

当然,特定程序的性能取决于程序本身的程度,而不仅仅是语言实现.语言实现仅设置了执行一系列操作的速度的上限.通常,您可以通过避免不必要的工作(即通过优化程序)来更好地提高程序的性能.无论您是通过解释器,JIT编译器还是预先编译器运行程序,都是如此.如果您想让某些事情变得更快,那么请不要全力以赴地实现更快的语言实现.有一些应用程序在解释和动态性方面并不可行,但它们并没有您想像的那么普遍(通常,通过有选择地调用由机器代码编译的代码来解决).

And of course, a specific program's performance depends on the program itself much more than the language implementation. The language implementation only sets an upper bound for how fast you can make a sequence of operations. Generally, you can improve the program's performance much better simply by avoiding unnecessary work, i.e. by optimizing the program. This is true regardless of whether you run the program through an interpreter, a JIT compiler, or an ahead-of-time compiler. If you want something to be fast, don't go out of your way to get at a faster language implementation. There are applications which are infeasible with the overhead of interpretation and dynamicness, but they aren't as common as you'd think (and often, solved by calling into machine code-compiled code selectively).

这篇关于Python 3解释器是否具有JIT功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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