Menu
Imam Ferianto Blogs
  • Home
  • About
Imam Ferianto Blogs

Speedup Python dengan Cython, Serba serbi compile python ke native c

Posted on May 13, 2020May 13, 2020 by feri

Cython adalah module python yang dapat menconversi source file python (.py atau .pyx) ke file kode file bahasa c. Dengan melakukan konversi ke c, maka dapat dibuat binary executable . Ini merupakan salah satu langkah mendistribusikan program yang dibuat dengan python sehingga source kode python kita aman, Selain itu dengan bentuk binary maka performance aplikasi akan naik karena tidak memerlukan precompile untuk menjalankanya.

#Requirement module:

brew install cython
pip3 install Cython

######## Setup path #####

nano /Users/feri/.zshrc

Tambakan kode berikut pada ahir baris:

export PATH=$PATH:/usr/local/Cellar/python@3.8/3.8.2/Frameworks/Python.framework/Versions/3.8/bin/

Jalankan source agar path dapat diload ke environtment:

source  /Users/feri/.zshrc

######## Compile Multi Module ########

#Untuk compile multi module, Buat file dengan nama: compile.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext_modules = [
    Extension("mymodule1",  ["mymodule1.py"]),
    Extension("mymodule2",  ["mymodule2.py"]),
#   ... all your modules that need be compiled ...
]

setup(
    name = 'My Program Name',
    cmdclass = {'build_ext': build_ext},
    ext_modules = ext_modules
)

#Cara mengcompile:

python3 compile.py build_ext --inplace

#Output (dapat dipanggil dengan perintah python import ):

mymodule1.py.xxxx.so

######## Compile Single Module ########

buat file dengan nama setup.py

from setuptools import setup
from Cython.Build import cythonize
setup(
    ext_modules = cythonize("main.py",compiler_directives={'language_level' : "3"})
)

Cara mengcompile:

python3 setup.py build_ext --inplace

Output:

build folder, file.so

 

######## Compile Native Binary C Standar ########

buat file: main.py

def main():
  print("hello python")
main()

konversi main.py ke main.c :

cython --embed -o main.c main.py

compile main.c ke main atau main.exe :

gcc `python2-config --cflags --ldflags` main.c -o main

#hasil Compile:

binary file: main atau main.exe

#Jalankan dengan :  ./main

#Screenshoot

 

#Notes: Bila ada error compile gcc, kemungkinan versi cython dan pythonnya tidak compatible

 

#Rereferensi:

https://medium.com/@xpl/protecting-python-sources-using-cython-dcd940bb188e
https://cython.readthedocs.io/en/latest/src/tutorial/cython_tutorial.html
https://riptutorial.com/cython
https://code.tutsplus.com/tutorials/speeding-python-with-cython–cms-29557
https://towardsdatascience.com/use-cython-to-get-more-than-30x-speedup-on-your-python-code-f6cb337919b6
http://okigiveup.net/an-introduction-to-cython/
https://github.com/adrn/cython-tutorial
https://realpython.com/cpython-source-code-guide/

Social Media Links

  • Linkedin Profile
  • Facebook Profile
  • Instagram Profile
  • GitHub Pages
  • Google Play Portofolio
  • Postgresql Training Service
  • PHP Security Training Service
  • Active State Writing

Recent Posts

  • Ngoding Data Analytics di Handphone Part 2
  • Ngoding Data Analytics di Iphone Part 1
  • Cara Mudah Membuat Report Dengan Fastreport pada dotnet core project
  • Cara Alternatif mengakses Webservice Soap pada dotnet core tanpa menggunakan soap client
  • Tutorial Yii2 Singkat padat dan jelas untuk programmer pemula. Bagian 1 – Membuat Aplikasi Penjualan Menggunakan yii2

Archives

  • September 2022
  • May 2021
  • April 2021
  • March 2021
  • January 2021
  • September 2020
  • July 2020
  • June 2020
  • May 2020
  • March 2020
  • February 2020
  • December 2019

Categories

  • #dotnetcore
  • arduino
  • c#
  • data-analytics
  • docker
  • oracle
  • php
  • programming
  • python
  • selingan
  • Uncategorized
©2023 Imam Ferianto Blogs | Powered by WordPress & Superb Themes