This article mainly introduces 5 ways to make Python code run faster, this article introduces the PyPy, Pyston, Nuitka, Cython, Numba and other open-source software, can improve the efficiency of Python, need friends can refer to the
Regardless of language, we need to pay attention to performance optimization problems, improve the efficiency of execution. The scripting language has to endure its speed, which in some ways illustrates the inadequacy of Python as a scripting language, which is that execution efficiency and performance are not bright enough. Although Python has never been faster than C and Java, many Python projects are in the forefront of developing languages.
Python is easy to use, but most people using python know that when working with intensive CPUs, it is still less than C, Java, and JavaScript in order of magnitude. But many third parties do not want to repeat Python's merits, but rather decide to improve their performance from the Inside out. If you want Python to run faster on the same hardware, you have two basic choices, and each has one drawback:
• You can create an alternative language (CPython implementation) that is used by default runtime--a major task, but it will ultimately be a simple replacement for CPython.
• You can also use some speed optimizer to rewrite existing Python code, which means that programmers need to devote more effort to writing code, but do not need to change at runtime.
How to optimize Python performance is the subject of this article. Here are five ways to improve the performance and execution efficiency of your Python code in some ways.
PyPy
When choosing a simple alternative to CPython, PyPy is undoubtedly the best choice (as Quora is written by it). Because it is highly compatible with existing Python code, PyPy is also a good choice for the default program to run.
PyPy uses the Just-in-time (JIT) Just-in-time compiler, the dynamic compiler, unlike a static compiler such as Gcc,java, which is optimized with data from the program's running process, with Google Chrome V8 JavaScript engine the same language acceleration technology. Earlier this month, the latest version, PyPy 2.5, is about to be released, with a series of performance improvements that provide more comprehensive, such as numpy support, to accelerate the shared library of Python performance.
Python 3.x must be built from a separate PyPy3 project. However, for the Edge language feature enthusiasts, despite the expected support for 3.3, the actual version supports only Python3.2.4 and the following versions.
Pyston
Pyston is a new JIT-based Python implementation of Dropbox, which uses the LLVM compiler to implement code parsing and transformation. Compared with PyPy, Pyston is still in its infancy, and the latest version is Pyston 0.2, which supports the related features of the limited subset language. The main tasks of Pyston include supporting the core functions of the language and improving the performance of key indicators to an acceptable level. Soon, Pyston can be considered a remote production ready language.
RELATED Links: Https://github.com/dropbox/pyston
Nuitka
Nuitka is a python alternative, and some teams are using it as a complete Python compilation tool and trying to translate Python code into other high-speed programming languages. Nuitka (nuitka.net) converts Python code into C + + code, then compiles it into an executable file, and implements transformations from the parsing language to the compiled language by directly invoking the Python API, and converting to C + + In the process of using Python's interpreter directly, you can guarantee that 100% of the syntax is compatible. Although this limits its portability, it is undeniable that the speed of the conversion has been affirmed.
Cython
Cython is the C language extension of Python. Exactly speaking, Cython is a separate language, designed to write an extension library for import in Python. In fact, Cython's syntax is basically consistent with Python, and Cython has a specific compiler: first converts the Cython code into C (automatically adding a whole bunch of c-python APIs) and then compiles the final Python callable module using the C compiler. The downside of Cython, though, is that you don't really write Python code, so that existing code will not completely automatically transfer success.
That is, Cython has a great advantage in speed-raising, a tool used to quickly generate a Python extension module (extention module). The types in cython,c, such as int,float,long,char*, are automatically converted to Python objects when necessary, or from Python objects to type C, which throws an exception when the conversion fails, which is the most magical place in Cython. In addition, Cython support for callback functions is also good. In short, if you have the need to write a Python extension module, then Cython is really a good tool.
Numba
Numba combines the first two methods and is a Cython competition project. Similarly, numba the Python source code to accelerate by generating the JIT. So file llvmpy. The difference is that Numba is JIT-oriented, accelerating the intrusion of the source code is small. Cython, however, focuses on the development of High-performance Python modules and does not rely on llvmpy projects. In addition, Numba is still immature, the current compatibility is quite poor.
RELATED Links: http://numba.pydata.org/
Python founder Guido van Rossum believes that many of Python's performance problems can be attributed to improper use of language. For example, there are ways to speed up Python operations with high CPU consumption-using numpy, using multiprocessor extensions, or using external C code to avoid the root cause of the slow--python of the Global Interpreter Lock (GIL). But with no viable Gil alternative language in Python, Python will still lag behind other languages in the short term-and possibly even longer.