5 ways to get Python code up and running faster

Source: Internet
Author: User
Tags numba
Regardless of the language, we need to pay attention to performance optimization problems, improve execution efficiency. Choosing a scripting language will endure its speed, which in some way 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 as fast as C and Java, many Python projects are in the lead of the development language.
Python is easy to use, but most people know that Python is still less than C, Java, and JavaScript when working with intensive CPUs. But many third parties do not want to repeat the advantages of Python, but decide to improve their performance from the Inside out. If you want Python to run faster on the same hardware, you have two basic options, each with one drawback:
• You can create a default runtime using an alternative language (CPython implementation)-a major task, but it will eventually only be a simple replacement for CPython.
• You can also use some speed optimizer to rewrite existing Python code, which means that programmers spend more time writing code, but don't need to change it at runtime.
How to optimize the performance of Python is the subject of this paper. Here are five ways to improve the performance and execution efficiency of Python code in some ways.

PyPy

When choosing a simple alternative language for 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 when the default program runs.
PyPy uses the Just-in-time (JIT) Instant compiler, a dynamic compiler, unlike a static compiler (such as Gcc,java, etc.), which is optimized using data from the program's running process, with Google Chrome V8 JavaScript engine the same language acceleration technology. Earlier this month, the latest version of PyPy 2.5 is about to be released, and this version will have a series of performance improvements that provide a more comprehensive set of shared libraries for accelerating Python performance, such as NumPy support.
The Python 3.x must be built from a separate PyPy3 project. However, for the Edge language feature enthusiast, although it is expected to support 3.3, the actual version only supports Python3.2.4 and below.
RELATED Links: http://pypy.org/

Pyston

Pyston is a new JIT-based Python implementation from Dropbox that implements code parsing and transformation using the LLVM compiler. 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. Pyston's main work includes supporting the core functions of the language and improving the performance of key indicators to an acceptable level. Soon, Pyston can be thought of as a remote production-ready language.
RELATED Links: Https://github.com/dropbox/pyston

Nuitka

Nuitka is a python replacement, 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) can convert Python code to C + + code, compile it as an executable, and convert from parsing language to compiled language by invoking the Python API directly, and converting to C + + In the process of using Python's interpreter directly, you can guarantee 100% syntax compatibility. Although this limits its portability, it is undeniable that the speed of the conversion has been affirmed.
RELATED Links: http://nuitka.net/

Cython

Cython is a C language extension of Python. To be exact, Cython is a separate language, designed to be used to write an extension library for import in Python. In fact, Cython's syntax is basically the same as Python, and Cython has a dedicated compiler that transforms the Cython code into C (which automatically joins a whole bunch of c-python APIs) and compiles the final Python-callable module using the C compiler. The downside of Cython, though, is that you can't actually write Python code, so the existing code won't completely automatically transfer success.
That said, Cython has a big advantage in speed-up, and it's a tool for quickly generating a Python extension Module (extention module). The cython,c type, such as int,float,long,char*, will automatically turn into a Python object when necessary, or from a Python object to a C type, which throws an exception when the conversion fails, which is the most magical part of Cython. In addition, Cython support for callback functions is also good. In short, if you have the need to write Python extensions, then Cython is really a great tool.
RELATED Links: http://cython.org/

Numba

Numba combines the first two methods and is a competitive project for Cython. Similarly, numba the Python source code by Llvmpy to generate a JIT. So file to speed up. The difference is that Numba is JIT-based, accelerating the intrusion of the source code is small. Cython is focused on accelerating the development of high-performance Python modules without relying on llvmpy projects. In addition Numba is very immature, the compatibility is quite poor at present.
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, for CPU-intensive processing, there are ways to speed up the Python run-using numpy, using multiprocessor extensions, or with external C code to avoid the slow root cause of the global Interpreter Lock (GIL)--python. But since there is no viable Gil alternative language in Python, Python will still lag behind other languages in the short term-perhaps 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.