Three-minute getting started with Cython

Source: Internet
Author: User
Tags acos
According to some of the feedback I received, it seems a bit confusing-Cython is used to generate C extensions instead of independent programs. All the acceleration is for a function of an existing Python application. Author: perrygeo
(Http://laiyonghao.com)
Http://www.perrygeo.net/wordpress? P = 116

My favorite is Python. Its code is elegant and practical. Unfortunately, it is slower than most languages in terms of speed. Most people think that the speed and ease of use are two poles-it is really painful to write C code. Cython tries to eliminate this dual nature and give you the Python syntax and C data types and functions at the same time-both of them are the best in the world. Remember, I am never an expert in this field. this is my first note on the true experience of Cython:

Edit: According to some feedback I have received, it seems a bit confusing-Cython is used to generate C extensions instead of independent programs. All the acceleration is for a function of an existing Python application. C or Lisp is not used to override the entire application, and C extensions are not handwritten. Just use a simple method to integrate the speed of C and the data type of C into Python functions.

Now we can say that we can make the following great_circle function faster. The so-called great_circle is used to calculate the distance between two points along the Earth's surface:

P1.py

Import math

Def great_circle (lon1, lat1, lon2, lat2 ):

Radius = 3956 # miles

X = math. pi/180.0

A = (90.0-lat1) * (x)

B = (90.0-lat2) * (x)

Theta = (lon2-lon1) * (x)

C = math. acos (math. cos (a) * math. cos (B) +

(Math. sin (a) * math. sin (B) * math. cos (theta )))

Return radius * c

Let's call it 0.5 million times and determine its time:

Import timeit

Lon1, lat1, lon2, lat2 =-72.345, 34.323,-61.823, 54.826

Num = 500000

T = timeit. Timer ("p1.great _ circle (% f, % f)" % (lon1, lat1, lon2, lat2 ),

"Import p1 ")

Print "Pure python function", t. timeit (num), "sec"

About 2.2 seconds. It's too slow!

Let's try to rewrite it with Cython quickly and see if there is any difference:
C1.pyx

Import math

Def great_circle (float lon1, float lat1, float lon2, float lat2 ):

Cdef float border radius = 3956.0

Cdef float pi = 3.14159265

Cdef float x = pi/180.0

Cdef float a, B, theta, c

A = (90.0-lat1) * (x)

B = (90.0-lat2) * (x)

Theta = (lon2-lon1) * (x)

C = math. acos (math. cos (a) * math. cos (B) + (math. sin (a) * math. sin (B) * math. cos (theta )))

Return radius * c

Please note that we still import math -- cython allows you to mix Python and C data types to a certain extent. The conversion is automatic, but it does not have no cost. In this example, we define a Python function, declare that its input parameter is of the floating point type, and declare the type of all variables as the C floating point data type. In the computing part, it still uses the math module of Python.

Now we need to convert it into C code and then compile it into Python extension. The best way to do this is to compile a release script named setup. py. However, we now use a manual method to learn about the Wizards:

# This will create a c1.c file-the C source code to build a python extension

Cython c1.pyx

# Compile the object file

Gcc-c-fPIC-I/usr/include/python2.5/c1.c

# Link it into a shared library

Gcc-shared c1.o-o c1.so

Now you should have a c1.so (or. dll) file that can be imported by Python. Run the following command:

T = timeit. Timer ("c1.great _ circle (% f, % f)" % (lon1, lat1, lon2, lat2 ),

"Import c1 ")

Print "Cython function (still using python math)", t. timeit (num), "s

About 1.8 seconds. There is no major performance improvement we expected at the beginning. Using python's match module should be a bottleneck. Now let's use the C standard library instead:

C2.pyx

Cdef extern from "math. h ":

Float cosf (float theta)

Float sinf (float theta)

Float acosf (float theta)

Def great_circle (float lon1, float lat1, float lon2, float lat2 ):

Cdef float border radius = 3956.0

Cdef float pi = 3.14159265

Cdef float x = pi/180.0

Cdef float a, B, theta, c

A = (90.0-lat1) * (x)

B = (90.0-lat2) * (x)

Theta = (lon2-lon1) * (x)

C = acossf (cosf (a) * cosf (B) + (sinf (a) * sinf (B) * cosf (theta )))

Return radius * cec"

Corresponding to import math, we use the cdef extern method to declare a function from the specified header file (here we use the math. h of the C standard library ). We replaced the expensive Python functions, created a new shared library, and re-tested:

T = timeit. Timer ("c2.great _ circle (% f, % f)" % (lon1, lat1, lon2, lat2 ),

"Import c2 ")

Print "Cython function (using trig function from math. h)", t. timeit (num), "sec"

Do you like it now? 0.4 seconds-5 times faster than pure Python functions. How can we increase the speed? C2.great _ circle () is still a Python function call, which means it generates Python API overhead (Build parameter tuples, etc.). if we can write a pure C function, we may be able to speed up.

C3.pyx

Cdef extern from "math. h ":

Float cosf (float theta)

Float sinf (float theta)

Float acosf (float theta)

Cdef float _ great_circle (float lon1, float lat1, float lon2, float lat2 ):

Cdef float border radius = 3956.0

Cdef float pi = 3.14159265

Cdef float x = pi/180.0

Cdef float a, B, theta, c

A = (90.0-lat1) * (x)

B = (90.0-lat2) * (x)

Theta = (lon2-lon1) * (x)

C = acossf (cosf (a) * cosf (B) + (sinf (a) * sinf (B) * cosf (theta )))

Return radius * c

Def great_circle (float lon1, float lat1, float lon2, float lat2, int num ):

Cdef int I

Cdef float x

For I from 0 <= I <num:

X = _ great_circle (lon1, lat1, lon2, lat2)

Return x

Note that we still have a Python function (def) that accepts an additional parameter num. The loop in this function uses for I from 0 <= I <num:, instead of more Pythonic, but the much slower for I in range (num ):. The actual computation is performed in the C function (cdef), which returns the float type. This version only takes 0.2 seconds-10 times faster than the original Python function.

To prove that we have done enough optimization, we can use pure C to write a small application, and then determine the time:

# Include

# Include

# Deprecision NUM 500000

Float great_circle (float lon1, float lat1, float lon2, float lat2 ){

Float radius = 3956.0;

Float pi = 3.14159265;

Float x = pi/180.0;

Float a, B, theta, c;

A = (90.0-lat1) * (x );

B = (90.0-lat2) * (x );

Theta = (lon2-lon1) * (x );

C = acos (cos (a) * cos (B) + (sin (a) * sin (B) * cos (theta )));

Return radius * c;

}

Int main (){

Int I;

Float x;

For (I = 0; I <= NUM; I ++)

X = great_circle (-72.345, 34.323,-61.823, 54.826 );

Printf ("% f", x );

}

Compile it with gcc-lm-o ctest. c, and test with time./ctest... about 0.2 seconds. This gives me confidence that my Cython extension is extremely efficient than my C code (not to say that my C programming skills are weak ).

The performance that can be optimized using cython usually depends on the number of cycles, numeric operations, and Python function calls, which will slow down the program. Some people have reported that in some cases, the speed has increased by 100 to 1000 times. Other tasks may not be so useful. Remember this before frantically using Cython to rewrite Python code:

"We should forget the small efficiency. Premature optimization is the root of all evil, as in 97% of cases. "-- Donald Knuth

In other words, you should first write a program in Python and then check whether it meets your needs. In most cases, its performance is good enough ...... But sometimes it is really slow, you can use the analyzer to find the bottleneck function, and then rewrite it with cython, so that you will soon be able to get higher performance.

External link
WorldMill (http://trac.gispython.org/projects/PCL/wiki/WorldMill)-A fast, concise python interface module written by Sean Gillies using Cython that encapsulates libgdal libraries for processing vector geospatial data.

Compile faster Pyrex code (http://www.sagemath.org: 9001/WritingFastPyrexCode) -- Pyrex, the predecessor of Cython, which has similar goals and syntax.

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.