Five methods to make Python code run faster

Source: Internet
Author: User
Tags numba
This article mainly introduces five methods to make Python code run faster. This article introduces open-source software such as PyPy, Pyston, Nuitka, Cython, and Numba, which can improve the running efficiency of Python, if you need it, you can refer to any language. We need to pay attention to performance optimization and improve execution efficiency. If you select a scripting language, you have to endure its speed. This statement illustrates to some extent the shortcomings of Python as a scripting language, that is, the execution efficiency and performance are not good enough. Although Python has never been as fast as C and Java, many Python projects are ahead of the development language.
Python is easy to use, but most people use Python and know that when processing intensive cpu jobs, it is still an order of magnitude lower than C, Java, and JavaScript. However, many third parties do not want to repeat the advantages of Python, but decide to improve its performance from the inside out. If you want Python to run faster on the same hardware, you have two basic options, and each has one disadvantage:
· You can create an alternative language used by default Runtime (CPython implementation)-a major task, but it will only be a simple replacement for CPython.
· You can also use some speed optimizers to rewrite existing Python code, which means that programmers need to spend more time writing code, but do not need to change it at runtime.
How to optimize Python performance is the topic of this article. The following five methods can improve the performance and execution efficiency of Python code in some aspects.

PyPy

PyPy is undoubtedly the best choice when selecting a simple alternative language for CPython (for example, Quora is compiled by it ). Because it is highly compatible with the existing Python code, PyPy is also a good choice for running the program by default.
PyPy uses the Just-in-Time (JIT) Real-Time compiler, that is, a dynamic compiler, which is different from a static Compiler (such as gcc and java, it optimizes the program running process data and has the same language acceleration technology as Google Chrome V8 JavaScript Engine. At the beginning of this month, the latest version of PyPy 2.5 is coming soon. This version will provide a series of performance improvements to provide more comprehensive support such as NumPy for accelerating the sharing library of Python performance.
Python 3.x must be built from a separate PyPy3 project. However, for edge language lovers, although it is expected to support 3.3, the actual version only supports Python 3.2.4 and earlier versions.
Link: http://pypy.org/

Pyston

Pyston is a new JIT-based Python implementation launched by Dropbox. It uses LLVM compiler to parse and convert code. Compared with PyPy, Pyston is still in its infancy. Currently, the latest version is Pyston 0.2, which supports the features of finite subset languages. Pyston's main work includes supporting the core features of the language and improving the performance of key indicators to an acceptable level. Soon, Pyston will be considered a remote production ready language.
Link: https://github.com/dropbox/pyston

Nuitka

Nuitka is a substitute for Python. Some teams are using it for full Python compilation tools and try to translate Python code into other high-speed programming languages. Nuitka (nuitka.net) can convert python code to C ++ code, compile it as an executable file, and directly call the python api to convert the parsing language to the compiling language, in addition, the python interpreter can be directly used during the conversion to C ++ to ensure 100% syntax compatibility. Although this limits its portability, it is undeniable that the conversion speed has been affirmed.
Link: http://nuitka.net/

Cython

Cython is a C Language extension of Python. Specifically, Cython is a separate language used to write the extension library used for import in Python. In fact, the syntax of Cython is basically the same as that of Python, while Cython has a special Compiler: first, Cython code is converted into C (a lot of C-Python APIs are automatically added ), then, use the C compiler to compile the final Python callable module. However, the disadvantage of Cython is that you cannot really write Python code, so that the existing code will not be fully automatically transferred.
That is to say, Cython has a great advantage in Speed-up. It is a tool used to quickly generate the Python extension module. The types in Cython and C, such as int, float, long, and char *, are automatically converted to python objects when necessary, or from python objects to C types, if the conversion fails, an exception is thrown, which is the most amazing part of Cython. In addition, Cython supports callback functions well. In short, if you need to write python extension modules, Cython is really a good tool.
Link: http://cython.org/

Numba

Numba combines the first two methods to compete with Cython. Similarly, numba uses the Python source code to generate the JIT. so file through LLVMPy to accelerate the process. The difference is that Numba is based on JIT, and acceleration is less invasive to source code. Cython focuses on Accelerating the Development of High-performance Python modules and does not rely on LLVMPy projects. In addition, numba is very immature and its compatibility is quite poor.
Link: http://numba.pydata.org/

Guido van rosum, founder of Python, believes that many of Python's performance problems can be attributed to improper use of the language. For example, for processing high CPU consumption, you can use some methods to accelerate Python running-using NumPy, using multi-processor extensions, or using external C code to avoid global interpreter locks (GIL) -- the root cause of Python slowness. But since there is no feasible GIL alternative language in Python, Python will still lag behind other languages in the short term-or even longer.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.