Python-accelerate programs with Cython

Introduction

Unlike C language, Python is an interpreted programming language that allows you to execute programs immediately without compiling. It's very convenient, but interpreter-type programming languages ​​run slower than languages ​​that require compilation, such as C. This time, I will introduce a library called Cython that can convert Python to C language files.

Program preparation

Preparing the program is easy, just change the extension of the program file from .py to .pyx

loop.py


for i in range(1000):
    print(i)

↓↓↓

loop.pyx


for i in range(1000):
    print(i)

If so, the file is ready

Preparing for Visual Studio 2019

Click the link below to download and install Visual Sudio 2019 Click here to go to the Visual Studio 2019 download page (https://visualstudio.microsoft.com/ja/downloads/)

Cython installation

With a command prompt

Command prompt


pip install cython

And press the enter key (return key)

Convert Python files to C language files

After that, move to the folder with the Python file with the command prompt, and

Command prompt


cythonize loop.pyx

Just type and press the enter key (return key)

Then loop.c will be completed immediately

Finally

Thank you for reading this article until the end This time, I introduced how to convert Python files to C language files with Cython. If you have any questions, please leave them in the comments. Thank you very much See you next time

Recommended Posts

Python-accelerate programs with Cython
Row profiling with Cython
Use Cython with Jupyter Notebook
Test Python non-functional programs with GitLab CI
Handle numpy with Cython (method by memoryview)
Array buffer object for use with Cython