The best way to make Python code run faster!

Python because of its powerful, flexible and easy-to-use and other characteristics, has earned its reputation. These advantages make it widely used in a variety of applications, workflows and fields. But on the design language that is dynamic it interpreted nature as well as in terms of its runtime, Python is always better than C or C ++ native machine language of such an order of magnitude slower.

Over the years, developers have proposed various workarounds for Python speed limit. For example, you can write in C and use performance-intensive tasks Python package it, many machine learning library doing just that. Or you can use Cython, this project can be kind of Python plus the type of information in order to compile the C run-time, in this way allows you to use Python code.

But workaround is never ideal. If we can run it faster as is the use of existing procedures and Python, it is not that nice? This is PyPy allows you to do things.

 

 

01

PyPy with CPython

 

PyPy the Python interpreter CPython direct replacement. CPython Python be compiled into intermediate byte code is then interpreted by the virtual machine, and PyPy using real-time (JIT) compiler to convert the Python code assembly language for the local machine.

According to the task being performed, performance may be very significant. On average, PyPy Python will accelerate by about 7.6 times, some of the tasks accelerated by 50 times or more. CPython interpreter will not perform the same optimization PyPy way and may never be, because this is not one of its design goals.

The best part is that developers need little or no effort to unlock the benefits PyPy provided. Just CPython replaced PyPy, and most have been completed. The following discusses some exceptions, but PyPy goal is to run existing and unmodified Python code and provide automation to enhance the speed.

PyPy currently supported by different versions of the project Python 2 and Python 3. In other words, you need to download a different version of PyPy, depending on the version of Python you're running. PyPy of Python 2 branch has existed for a long time, but so far, python 3 version of speed has been improved a lot. PyPy currently supports Python 3.5 (release version) and Python 3.6 (beta version).

In addition to supporting all core Python language outside, PyPy can also be used in conjunction with the Python ecosystem of the vast majority of tools, such as packaged for virtualenv pip or for virtual environments. Most Python packages, even those with a C module package, as it will run. Of course, there are some restrictions, we will introduce some restrictions below.

 

 

02

How PyPy work

 

PyPy use other instant dynamic language compiler optimization techniques. It analyzes Python program run to determine the type of information to create and use objects in the program, and then use that information as a guide to the type of speed. For example, if the Python functions using only one or two different types of objects, PyPy generates machine code to handle these particular circumstances.

PyPy optimization is handled automatically at runtime, so you usually do not need to adjust its performance. Advanced users may try to use PyPy command-line option to generate faster code as a special case, but this is usually rarely required.

PyPy also out of the way CPython deal with some internal function, but it also attempts to preserve compatible behavior. E.g. PyPy garbage collection process CPython different way. Not all objects recovered immediately once it is out of range, so Python program running under PyPy may take up more memory than running show in CPython time. But you can still use the advanced Python garbage collection controls by public gc module, for example gc.enable (), gc.disable () and gc.collect () and so on.

If you want to get information about PyPy at runtime JIT (real time) the behavior of information, PyPy contains a module pypyjit, which discloses a number of JIT-related information to your Python applications. If one of your functions or modules poor performance on JIT, then pypyjit allows you to get detailed statistics about it.

Another PyPy-specific modules, __ pypy__ exposed the other features PyPy unique, and therefore very useful for writing applications use these features. Due to the dynamic nature of Python is running, it is possible to build Python applications that use these functions when PyPy exist, and ignore them when it is not present.

 

 

03

PyPy restrictions

 

PyPy may look like a magic, like magic, but in fact it is not magic. PyPy also has some limitations, can weaken or eliminate the effectiveness of some programs. Oh, completely generic alternatives PyPy than CPython runtime.

PyPy most suitable for pure Python application

PyPy best performance in "pure" Python application, in other words that is written in Python and not mixed with other application languages ​​in the best performance. Since the present embodiment PyPy mimic the CPython native binary interface, and the C library (e.g. NumPy) Python packet interface nor the less outstanding.

PyPy developers have solved this problem, and the most dependent on PyPy C extensions Python packages more compatible. For example Numpy now compatible with PyPy very good. However, if you want to extend maximum compatibility with C, use CPython.

PyPy suitable for a long-running program

One side effect of optimizing Python programs PyPy is the most long-running program to benefit by optimizing the PyPy. The longer running time, the more run-time type information PyPy can collect, the more of it can be optimized. Once and for all Python script will not benefit from such things. For example benefit Python applications usually have behavior long cycle operation, or run continuously in the background Web framework.

PyPy no precompiled

PyPy compile Python code, but it's not Python code compiler. Because PyPy implementation of its optimized way and the inherent dynamic nature of Python, and therefore can not be generated JITted code as a separate binary file and re-emit use it. Each run must compile each program. If you want to compile Python to faster code can be run as a standalone application, then still use Cython, Numba or current Nuitka experimental projects.

Guess you like

Origin www.cnblogs.com/mo3408/p/10931879.html