Hackspeed with Cython

Tram Ho

Python is one of the most popular and popular programming languages ​​today. However, when working with Python, you will encounter or be told about one of the weaknesses up to this point: Python is slow .

There are several ways to speed up your Python code. Maybe you read it somewhere:

  • Using multi-processing libraries
  • Using asynchronous

You can read more of his article here .

As above, you will approach 2 sides to speed up Python code: parallel programming and asynchronous programming . Now, I will introduce a different approach. It was Cython .

What’s Cython?

Can understand Cython is an intermediate step between Python and C / C ++. It allows you to write pure Python with some minor modifications, then translate it directly into C.

Cython will bring you the combined power of Python and C:

  • Python code calls back and forth C or C ++ native code at any time.
  • Easily modify Python code for performance like C code simply by adding static type declarations, also in Python syntax.
  • Interact effectively with the big data set.
  • Integrate native with existing code, low-level or high-performance libs / apps.

Some other information:

You can easily the Cython via pip

Compared to Python code, you need to add type information to every variable. Usually, to declare a Python variable, very simple:

With Cython, you need to add the type for that variable:

Just like in C, the type declaration for a variable in Cython is required.

Types in Cython

When using Cython, there are two different points for variables and functions.

For variable:

All of these types are derived from C / C ++.

For function:

With:

  • def: Function python pure, only called from Python.
  • cdef: Cython only functions. Only called from Cython.
  • cpdef: C and Python function. Can be called from C and Python.

How to speedup your code with Cython

First, I will create a pure Python code with for-loop.

Applying what I have understood above, I will write the code in Cython with a meaning:

When coding Cython, make sure that all of your variables are set.

Next, we need to create a file to compile from Cython -> C code:

After putting run_test_cython.pyx and setup.py with dir, we start to compile:

Result:

You will see in this folder that contains all the files needed to run C code. If you’re curious what the other Cython code will compile into is what C code you can cat to see that file:

Come on, it’s time to show the power of C code. The following code compares the speed of Python pure and Cython:

Results after running speedtest.py

With the current machine configuration for my test:

  • CPU: Intel® Core ™ i5-4460 CPU @ 3.20GHz × 4
  • Ram: 8G

Fill the results in the table for easy viewing:

NumberPython timeCython timeSpeedup
ten3.5762786865234375e-067.152557373046875e-075.0
1007.867813110351562e-062,384185791015625e-0733.0
10000.00028109550476074229.5367431640625e-07294.75
100000.021443367004394537.62939453125e-062810,625
1000003.11714386940002448.630752563476562e-0536116.70994475138

36116 – Seems like an unthinkable number ? .

Clearly, Cython gives you very good performance. This is also a viable solution if you want to improve your code.

Source: https://cython.org/

Thanks for reading!

Share the news now

Source : Viblo