Cython, accelerated Python, Protection Code (2): Faster the code via static Typing__python

Source: Internet
Author: User
Tags wrapper


Http://docs.cython.org/src/quickstart/cythonize.html


Cython is a Python compiler. This means the it can compile normal Python code without changes (with a few obvious exceptions of some As-yet D language Features). However, for performance critical code, it are often helpful to add static type declarations, as they would allow Cython to Step out of the "dynamic nature of" the Python code and generate simpler and faster C code-sometimes faster by orders of M Agnitude.

Cython can be completely compatible with Python, but the declared variable category displayed is a great help to Cython:

1 eliminate the dynamic characteristics of Python code

2 Generate faster C code


Common type declarations include variable declarations and function declarations.



Raw Python code:

def f (x): Return
    x**2-x

def integrate_f (A, B, N):
    s = 0
    dx = (b-a)/n for
    I in range (N): s = =
        F (a+i *DX) return
    S * DX
Directly compiled into Cython, there are 35% of speedup.




Declaring variable types:

def f (Double x): Return
    x**2-x

def integrate_f (double A, double b, int N):
    cdef int i
    cdef double s, dx< C12/>s = 0
    dx = (b-a)/n for
    I in range (N):
        S + + f (A+I*DX) return
    S * DX
It is critical to participate in a variable that is similar to a cyclic calculation such as I,A,S,DX. Even though N and B are less critical, it is recommended that you declare them.

Speed up 4 times times.



function declaration Type:

Using Cdef,cpdef

Cdef double F (double x) except? -2: Return
    x**2-x
150 times times higher speed.

The downside: The Python compiler no longer recognizes the function (without knowing how to invoke the Cdef function), Python dynamic-like features are no longer available (it is impossible to change the F () function at runtime time, fixed-position double).


The difference between using cpdef: using the CPDEF keyword instead of cdef, a Python wrapper is also created, so this function is available bo Th from Cython (fast, passing typed values directly) and to Python (wrapping values in Python objects). In fact, Cpdef does is not just provide a Python wrapper, it also installs to logic the "method" to "allow by overridden On methods, even as called from within Cython. This does add a tiny overhead compared to cdef methods.

1 The simultaneous generation of Python wrapper;2) so that functions can be overridden




Decide when to add a type declaration:

Because static typing is often the key to large speed gains, beginners often, have a tendency to type everything in sight. This both readability and flexibility and can even slow things-e.g. by adding unnecessary type checks, conversions, or slow buffer unpacking). On the other hand, it's easy to kill performance by forgetting to type a critical loop variable. Two essential tools to help the this task are profiling and annotation. Profiling should be the "the" the "the" the "the" the ' any optimization effort, and can tell you where you are spending. Cython ' s annotation can then tell your why your code is taking time.

Using the-a Switch to the Cython command line program (or following a link from the Sage notebook) results in an HTML rep ORT of Cython code interleaved with the generated C code. Lines are colored according to the level of ' typedness ' –white Lines translate to pure C, while Lines that require the Py Thon C-api are yellow (darker as they translate to more c-api). Lines that translate-C code have a plus (+) in front and can is clicked to show the generated code.

This are invaluable when optimizing a function for speed, and for determining then to release the Gil:in General, a Nogil block could contain only the "white" code.





Here's a good one:

Http://blog.septicmk.com/Python/use-Cython.html

If you need to add C/s code to Python code, I think the top priority should be Cython, and of course there are other ways, as this article says. In addition to being able to invoke native-C + + modules, Cython can also allow you to write extensions of C/A + + in Python syntax.  But there are a lot of tips and tricks worth noting. In this article python==2.7.10, cython=0.22.1 Cython How to work?

First, Python-c-api is the native Python module of the Python interpreter, which allows you to write Python extensions using C + +. But in order for Python to make a successful call to the C + + module, you have to spend a lot of time writing low-level controls to wrap the original code. What Cython is doing is compiling and using PYTHON-C-API to help you complete the work and eventually weave it into a shared library file (. So). So you can import it directly through the import in Python code.

In general, two common scenarios for using the C + + module in Python are that the original Python code is too poor to have a ready-made C/s + + direct call

First of all, you have to write a. pyx file, write the external call interface function, and then a setup.py file that, if it goes well, generates a shared library file (. So) that you can import into the Python code.
For example, if you want to write an add (x,y) function, first:

1
2
3
# Add.pyx
def add (x, y): return
    x + y

Then a compiled file:

1
2
3
4
5
6 7 8
# setup.py
from Distutils.core Import setup
from cython.build import cythonize

setup (
  name = ' Add ', C12/>ext_modules = Cythonize ("Add.pyx"),
)

Then compile:

1
Python setup.py build_ext--inplace

Where you need to call:

1
2
3
# main.py
Import add
print Add.add (1,-1)
using Cython syntax Defining Variables

Use Cdef to define variables, structs, constants.

1
2
3
4
5
6 7
cdef int i, *J, k[100]
cdef struct node:
    int key
    float value
cdef enum:
    const = 1
CDEF bint Fla G # BOOL type uses bint instead

You can use a cdef to write them together:

1
2
3
4
5
6 7
Cdef:
    cdef struct node:
        int key
        float value
    int i, *J, k[100]
    void Add (int x, int y):
        retur n x + y

You can also use Ctypedef to define the type name.

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.