معرفی زبان برنامه نویسی Cython

معرفی زبان برنامه نویسی Cython

باسلام و عرض ادب. در این مطلب در خدمت شما هستیم با مقاله معرفی زبان برنامه نویسی Cython از وب سایت آموزش برنامه نویسی سورس باران. cython (به فارسی : سایتون ) یک زبان برنامه‌ نویسی برای توسعه ماژول پایتون می‌باشد. نحوه کار زبان برنامه نویسی سایتون به این شکل است که ابتدا برنامه‌ نویس ماژول خود را با قوانین نوشتاری سایتون و با پسوند .pyx مینویسد سپس یک فایل برای نصب آن با پایتون میسازد که سایتون را فراخوانی کرده باشد سایتون ماژول را به C ترجمه می‌کند و متن توسط کامپایلر تبدیل به ماژول قابل استفاده می‌شود. لطفا تا انتها با ما همراه باشید…

زبان برنامه نویسی Cython

سایتون (Cython) توسعه داده شده است تا امکان ساخت افزونه‎های C را برای پایتون ساده‎تر کند و به کدهای موجود پایتون اجازه دهد تا به C تبدیل شوند. علاوه بر این، سایتون این امکان را برای کدهای بهینه شده فراهم می‎کند تا بدون وابستگی‎های خارجی با پایتون همراه شوند. در این مقاله ما مراحلی را که لازم است تا کدهای موجود پایتون به سایتون تبدیل شده و در یک اپلیکیشن کاربردی مورد استفاده قرار گیرند، بررسی خواهیم کرد.پایتون یک زبان برنامه نویسی قدرتمند است که یادگیری و کار با آن حتی برای افراد مبتدی راحت است، اما از طرفی همیشه اجرای آن به ویژه وقتی با ریاضیات و آمار سر و کار دارید با سرعت انجام نمی‎شود. کتابخانه‎های ثالثی مثل NumPy که کتابخانه‎های C را پوشش می‎دهد می‎تواند عملکرد بعضی از عملیات‎ها را به طور قابل توجهی بهبود بخشد، اما بعضی اوقات شما مستقیما به سرعت خام و قدرت C در پایتون نیاز دارید.

این یک مثال ساده برای ساخت یک ماژول با سایتون است که بتواند عبارت “hello world” را چاپ کند. این متن اصلی ماژول است که قرار است توسط سایتون به C ترجمه شود.

# hello.pyx 
def say_hello():
    print "Hello World!"

این فایل نصبی است که سایتون را فراخوانی می‌کند

# setup.py 
from distutils.core import setup
from Cython.Build import cythonize

setup(name = 'Hello world app',
      ext_modules = cythonize("*.pyx"))

این فایلی است که ماژول را در پایتون فراخوانی می‌کند.

# launch.py  
# This code is always interpreted, like normal Python.
# It is not compiled to C.

import hello
hello.say_hello()

سپس این دو دستور در خط فرمان (sh/bash) ماژول را ساخته و فراخوانی میکنند

حتما بخوانید : فیلم آموزش برنامه نویسی سایتون (Cython)

مثالی 2 از برنامه نویسی Cython

اجازه دهید در ابتدا با یک مثال ساده که از اسناد سایتون گرفته شده کار را آغاز کنیم، یک پیاده سازی نه چندان کارآمد از یک تابع انتگرال :

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

 

خواندن و درک این کد راحت است، اما اجرای آن به کندی صورت می‎گیرد. این به دلیل آن است که پایتون باید به طور دائم بین انواع شی‎های خود و انواع مقادیر عددی ماشین در رفت و آمد باشد. حالا نسخه سایتون همین کد را در نظر بگیرید که در اینجا زیر بخش‎های اضافه شده سایتون خط کشیده شده است :

cdef f(double x):
    return x**2-x

def integrate_f(double a, double b, int N):
    cdef int i
    cdef double s, x, dx
    s = 0
    dx = (b-a)/N
    for i in range(N):
        s += f(a+i*dx)
    return s * dx

این بخش‎های اضافی به ما اجازه می‎دهد در میان این کد صریحا انواعی از متغیرها را تعریف کنیم، به این شکل کامپایلر سایتون می‎تواند این بخش‎های اضافه را به C تفسیر کند.

 

نوع توابع cdef و cpdef

کلیدواژه cdef استفاده از نوع سایتون یا C را مشخص می‎کند. همچنین از آن برای تعریف توابعی که شما مایلید در پایتون داشته باشید، استفاده می‎شود. توابعی که در سایتون با استفاده از کلیدواژه پایتون def نوشته می‎شوند برای سایر کدهای پایتون قابل دسترس هستند، اما با محدودیت‎های وضعیت عملکرد پایتون مواجه می‎شوند. توابعی که از کلیدواژه cdef استفاده می‎کنند تنها برای سایر کدهای سایتون و C قابل دسترس هستند، اما خیلی سریع‎تر اجرا می‎شوند. اگر شما توابعی دارید که تنها به صورت داخلی در خود ماژول سایتون فراخوانی می‎شوند، از cdef استفاده کنید.

سومین کلیدواژه cpdef است که امکان سازگاری با کدهای پایتون و کدهای C را به شیوه‎ای که کدهای C می‎توانند با حداکثر سرعت به این تابع دسترسی داشته باشد فراهم می‎کند. هر چند این سهولت در کار پیامدهایی را نیز در پی دارد: توابع cpdef کدهای بیشتری تولید می‎کنند و نسبت به cdef فراخوانی آنها منابع بیشتری نیاز دارد. سایر کلیدواژه‎های سایتون سایر کلمات کلیدی در سایتون کنترل جنبه‎های مختلف چرخه برنامه را که در پایتون وجود ندارد، برعهده دارند:

  • Gil و nogil مدیریت منابعی را برعهده دارند که برای معین کردن بخش‎هایی از کد که به Global Interpreter Lock پایتون نیاز دارد (با gil:) یا نیاز ندارد (با nogil:) استفاده می‎شود. کدهای C که هیچ فراخوانی به API پایتون ندارند، در یک بلوک nogil سریع‌تر اجرا می‎شوند، به ویژه اگر یک عملیات طولانی را اجرا کنند.
  • از cimport برای نظارت بر ورود نوع داده، تابع، متغیرها و نوع افزونه C به سایتون استفاده می‎شود. برای نمونه، اپلیکیشن‎های سایتون که از ماژول NumPy اصلی C استفاده می‎کنند، از cimport برای فراهم کردن امکان دسترسی به این توابع استفاده می‎کنند.
  • Include کد منبع یک فایل سایتون را به همان شیوه موجود در C به کدهای دیگر اضافه می‎کند. توجه داشته باشید که سایتون از یک روش پیچیده‎تری برای اشتراک گذاشتن عبارات بین فایل‎های سایتون نسبت به include استفاده می‎کند.
  • از ctypedef برای ارجاع به هدر فایل‎های خارجی C استفاده می‎شود.
  • از Extern با cdef استفاده می‎شود تا به توابع C یا متغیرهای پیدا شده در سایر ماژول‎ها مراجعه کند.
  • از public/api استفاده می‎شود تا عباراتی را در ماژول‎های سایتون ایجاد کند که می‎توان در سایر کدهای C به آن دسترسی داشت.
  • از inline استفاده می‎شود تا یک تابع را به صورت خطی تعریف کرد، و یا کدهای آن را به منظور افزایش سرعت در بدنه تابع فراخوانی شده به کار گرفت. برای نمونه تابع f در کد مثال بالا می‎تواند به inline مجهز شود تا سرعت فراخوانی را افزایش دهد، زیرا از آن تنها در یک مکان استفاده می‎شود.

نیازی نیست که پیشاپیش از تمام کلمات کلیدی سایتون آگاه باشید. کدهای سایتون به شکلی در نظر گرفته شده‎اند که می‎توان آنها را به تدریج نوشت. ابتدا شما کد پایتون را می‎نویسید، سپس ضمایم سایتون را به منظور افزایش سرعت به آن اضافه می‎کنید. به این شکل شما می‎توانید به تدریج کلیدواژه فرامین دستوری سایتون را برداشته و بر اساس نیاز خود استفاده کنید.

 

کامپایل Cython

حالا که با نحوه عملکرد یک برنامه ساده سایتون آشنا شدید، در ادامه قصد داریم مراحل مورد نیاز را برای کامپایل سایتون به یک فایل باینری قابل اجرا بررسی کنیم. برای ساخت یک برنامه قابل اجرای سایتون ما به سه چیز نیاز خواهیم داشت:

  1. مفسر پایتون، در صورت امکان از آخرین نسخه منتشر شده استفاده کنید.
  2. شما می‎توانید با استفاده از روش مدیر پکیج pip به صورت pip install cython سایتون را به پایتون اضافه کنید.
  3. یک کامپایلر C

اگر شما از ویندوز مایکروسافت به عنوان پلتفرم توسعه خود استفاده می‎کنید باید گزینه شماره 3 را مد نظر داشته باشید. برخلاف لینوکس، ویندوز به عنوان یک تجهیزات استاندارد به کامپایلر C مجهز نیست. برای دستیابی به این ویژگی یک کپی از Microsoft Visual Studio Community Edition انتخاب کنید که با کامپایلر C مایکروسافت همراه است و هزینه‎ای برای شما به همراه ندارد. برنامه‎های سایتون از پسوند فایل .pyx استفاده می‎کنند. در یک دایرکتوری جدید یک فایل به نام num.pyx ایجاد کنید که شامل کد سایتون نمایش داده شده در مثال بالا است و یک فایل دیگر به نام main.py بسازید که شامل کد زیر است :

from num import integrate_f
print (integrate_f(1.0, 10.0, 2000))

این یک برنامه عادی پایتون است که تابع integrate_f  موجود در num.pyx را فراخوانی می‎کند. کد پایتون با کد سایتون تنها به شکل یک ماژول دیگر برخورد می‎کند، بنابراین شما به جز وارد کردن این ماژول کامپایل شده و اجرای توابع آن نیاز نیست کار خاص دیگری انجام دهید.
سرانجام، یک فایل دیگر با نام setup.py با کد زیر اضافه کنید:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize

ext_modules = [
Extension(
r’num’,
[r’num.pyx’]
),
]

setup(
name=’num’,
ext_modules=cythonize(ext_modules),
)

 

 

setup.py در حالت عادی توسط پایتون برای نصب ماژول همراه با آن استفاده می‎شود و همچنین می‎توان از آن برای کامپایل مستقیم افزونه‎های C در پایتون استفاده کرد. در اینجا ما از setup.py برای کامپایل کدهای سایتون استفاده می‎کنیم.
اگر شما از لینوکس استفاده می‎کنید و یک کامپایلر C نصب شده دارید می‎توانید فایل .pyx را با اجرای این فرمان به C کامپایل کنید:

> python setup.py build_ext --inplace

 

اگر از ویندوز استفاده می‎کنید باید یک فایل بچ به نام compile.bat اضافه کنید تا فرآیند کامپایل را خودکارسازی کند:

@SETLOCAL
set DISTUTILS_USE_SDK=1
call “C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat” amd64
python setup.py build_ext --inplace --compiler=msvc

 

توجه داشته باشید که مسیر درست به vcvarsall.bat در خط 3 به نسخه ویژوال استودیو که شما نصب کرده‎اید، بستگی دارد. فرض این مثال بر این است که شما از Visual Studio 2017 Community استفاده کرده‎اید. اگر عمل کامپایل با موفقیت انجام شود، شما باید فایل‎های جدیدی را در این دایرکتوری مشاهده کنید: num.c (فایل C تولید شده توسط سایتون) و یک فایل با پسوند .o (در لینوکس ) یا .pyd (در ویندوز). این همان فایل باینری است که فایل C به آن کامپایل شده است. شما ممکن است یک دایرکتوری به نام \build را نیز مشاهده کنید که شامل مصنوعاتی از فرآیند ساخت است. فرمان python main.py را اجرا کنید تا به عنوان پاسخ برگشتی چیزی شبیه به این را مشاهده کنید:

283.297530375

این همان خروجی تابع انتگرال کامپایل شده که توسط کد اصلی پایتون ما فراخوانی شده است. با پارامترهای این تابع در main.py بازی کنید تا ببینید این خروجی چگونه تغییر می‎کند. توجه داشته باشید هر زمان که شما تغییراتی را به فایل .pyx اعمال می‎کنید باید آن را دوباره کامپایل کنید. مسلما هر تغییری که شما روی کدهای معمول پایتون اعمال کنید فورا تاثیرگذار خواهد بود.نتیجه فایل کامپایل شده هیچ وابستگی بجز نسخه پایتونی که برای آن کامپایل شده ندارد و به همین دلیل می‎توان آن را به یک binary wheel الصاق کرد. توجه داشته باشید که اگر شما در کد خود به سایر کتابخانه‎ها مثل NumPy  (در ادامه به آن خواهیم پرداخت) ارجاع داشته باشید باید آنها را به عنوان بخشی از عناصر مورد نیاز اپلیکیشن خود فراهم کنید.

 

چگونه از سایتون استفاده کنیم 

حالا که متوجه شدیم چگونه می‎توان یک کد را به سایتون تبدیل کرد، مرحله بعدی این است که ببینیم چگونه اپلیکیشن پایتون شما می‎تواند از مزایای سایتون استفاده کند. و دقیقا کجا باید آن را به کار بگیرید؟ برای دریافت نتیجه بهتر، از سایتون برای بهینه سازی این نوع از توابع پایتون استفاده کنید :

  • توابعی که در چرخه‎های فشرده اجرا می‎شوند یا در یک بخش مهم و حیاتی از یک کد نیاز به مقدار زیادی زمان پردازش دارند.
  • توابعی که به انجام اعمال عددی می‎پردازند.
  • توابعی که با شی‎هایی کار می‎کنند که در زبان C وجود دارند. مثل انواع عددی، آرایه‎ها یا ساختارها به جای انواع شی‎های پایتون مثل لیست‎ها، دیکشنری‎ها یا تاپل‎ها.

پایتون نسبت به سایر زبان‎های غیر تفسیری اصولا در چرخه‎ها و انجام اعمال عددی کارایی پایین‎تری دارد. هر چه شما بیشتر از انواع عددی تبدیل شده به C در کد خود استفاده کنید، محاسبات عددی سریع‎تر انجام خواهد شد. استفاده از انواع شی‎های پایتون در سایتون به خودی خود مشکل‌ساز نیست. توابع سایتون که از شی‎های پایتون استفاده می‎کنند همچنان کامپایل می‎شوند و زمانی که عملکرد اولویت اصلی نیست شی‎های پایتون ترجیح داده می‎شوند. اما هر کدی که از شی‎های پایتون استفاده می‎کند با مشکلات عملکرد ذاتی پایتون مواجه می‎شود زیرا سایتون کدها را مستقیم بر اساس API و ABIهای پایتون تولید خواهد کرد. یکی دیگر از اهداف ارزشمند بهینه‌سازی با سایتون کدهای پایتونی است که مستقیم با یک کتابخانه C در تعامل است. شما می‎توانید از کد wrapper پایتون صرف نظر کرده و مستقیم با کتابخانه‎ها در ارتباط باشید. اما سایتون نمی‎تواند به طور خودکار فراخوانی مناسب را برای ارتباط با این کتابخانه‎ها تولید کند. شما باید سایتون را به شکلی تنظیم کنید که در هدر فایل‎های کتابخانه‎ها به توابع ارجاع داده شود. روش انجام این کار استفاده از دستور cdef extern from است.

یکی از کتابخانه‎های خارجی C که سایتون می‎تواند مستقیم از آن استفاده کند NumPy است. برای بهره‌مند شدن از دسترسی سریع سایتون به آرایه‎های NumPy از cimport numpy استفاده کنید و سپس از دستورالعمل‎های cdef برای مشخص کردن متغیرهای NumPy از قبیل cdef np.array یا np.ndarray استفاده کنید.

 

توضیحات انگلیسی و منابع

What is Cython? Python at the speed of C

Python has a reputation for being one of the most convenient, richly outfitted, and downright useful programming languages. Execution speed? Not so much.

Enter Cython. The Cython language is a superset of Python that compiles to C, yielding  performance boosts that can range from a few percent to several orders of magnitude, depending on the task at hand. For work that is bound by Python’s native object types, the speedups won’t be large. But for numerical operations, or any operations not involving Python’s own internals, the gains can be massive. This way, many of Python’s native limitations can be routed around or transcended entirely.

With Cython, you can skirt many of Python’s native limitations or transcend them entirely—without having to give up Python’s ease and convenience. In this article, we’ll walk through the basic concepts behind Cython and create a simple Python application that uses Cython to accelerate one of its functions.

Table of Contents

  • Compile Python to C
  • How to use Cython
  • Cython advantages
  • Cython limitations
  • Cython NumPy 
Show More

Compile Python to C

Python code can make calls directly into C modules. Those C modules can be either generic C libraries or libraries built specifically to work with Python. Cython generates the second kind of module: C libraries that talk to Python’s internals, and that can be bundled with existing Python code.

Cython code looks a lot like Python code, by design. If you feed the Cython compiler a Python program, it will accept it as-is, but none of Cython’s native accelerations will come into play. But if you decorate the Python code with type annotations in Cython’s special syntax, Cython will be able to substitute fast C equivalents for slow Python objects.

Note that Cython’s approach is incremental. That means a developer can begin with an existing Python application, and speed it up by making spot changes to the code, rather than rewriting the whole application from the ground up.

This approach dovetails with the nature of software performance issues generally. In most programs, the vast majority of CPU-intensive code is concentrated in a few hot spots—a version of the Pareto principle, also known as the “80/20” rule. Thus most of the code in a Python application doesn’t need to be performance-optimized, just a few critical pieces. You can incrementally translate those hot spots into Cython, and so get the performance gains you need where it matters most. The rest of the program can remain in Python for the convenience of the developers.

How to use Cython

Consider the following code, taken from Cython’s documentation:

Python has a reputation for being one of the most convenient, richly outfitted, and downright useful programming languages. Execution speed? Not so much.

Enter Cython. The Cython language is a superset of Python that compiles to C, yielding  performance boosts that can range from a few percent to several orders of magnitude, depending on the task at hand. For work that is bound by Python’s native object types, the speedups won’t be large. But for numerical operations, or any operations not involving Python’s own internals, the gains can be massive. This way, many of Python’s native limitations can be routed around or transcended entirely.

With Cython, you can skirt many of Python’s native limitations or transcend them entirely—without having to give up Python’s ease and convenience. In this article, we’ll walk through the basic concepts behind Cython and create a simple Python application that uses Cython to accelerate one of its functions.

Table of Contents

  • Compile Python to C
  • How to use Cython
  • Cython advantages
  • Cython limitations
  • Cython NumPy 
Show More

Compile Python to C

Python code can make calls directly into C modules. Those C modules can be either generic C libraries or libraries built specifically to work with Python. Cython generates the second kind of module: C libraries that talk to Python’s internals, and that can be bundled with existing Python code.

Cython code looks a lot like Python code, by design. If you feed the Cython compiler a Python program, it will accept it as-is, but none of Cython’s native accelerations will come into play. But if you decorate the Python code with type annotations in Cython’s special syntax, Cython will be able to substitute fast C equivalents for slow Python objects.

Note that Cython’s approach is incremental. That means a developer can begin with an existing Python application, and speed it up by making spot changes to the code, rather than rewriting the whole application from the ground up.

This approach dovetails with the nature of software performance issues generally. In most programs, the vast majority of CPU-intensive code is concentrated in a few hot spots—a version of the Pareto principle, also known as the “80/20” rule. Thus most of the code in a Python application doesn’t need to be performance-optimized, just a few critical pieces. You can incrementally translate those hot spots into Cython, and so get the performance gains you need where it matters most. The rest of the program can remain in Python for the convenience of the developers.

How to use Cython

Consider the following code, taken from Cython’s documentation:

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

This is a toy example, a not-very-efficient implementation of an integral function. As pure Python code, it’s slow, because Python must convert back and forth between machine-native numerical types and its own internal object types.

Now consider the Cython version of the same code, with Cython’s additions underscored:

Python has a reputation for being one of the most convenient, richly outfitted, and downright useful programming languages. Execution speed? Not so much.

Enter Cython. The Cython language is a superset of Python that compiles to C, yielding  performance boosts that can range from a few percent to several orders of magnitude, depending on the task at hand. For work that is bound by Python’s native object types, the speedups won’t be large. But for numerical operations, or any operations not involving Python’s own internals, the gains can be massive. This way, many of Python’s native limitations can be routed around or transcended entirely.

With Cython, you can skirt many of Python’s native limitations or transcend them entirely—without having to give up Python’s ease and convenience. In this article, we’ll walk through the basic concepts behind Cython and create a simple Python application that uses Cython to accelerate one of its functions.

Table of Contents

  • Compile Python to C
  • How to use Cython
  • Cython advantages
  • Cython limitations
  • Cython NumPy 
Show More

Compile Python to C

Python code can make calls directly into C modules. Those C modules can be either generic C libraries or libraries built specifically to work with Python. Cython generates the second kind of module: C libraries that talk to Python’s internals, and that can be bundled with existing Python code.

Cython code looks a lot like Python code, by design. If you feed the Cython compiler a Python program, it will accept it as-is, but none of Cython’s native accelerations will come into play. But if you decorate the Python code with type annotations in Cython’s special syntax, Cython will be able to substitute fast C equivalents for slow Python objects.

Note that Cython’s approach is incremental. That means a developer can begin with an existing Python application, and speed it up by making spot changes to the code, rather than rewriting the whole application from the ground up.

This approach dovetails with the nature of software performance issues generally. In most programs, the vast majority of CPU-intensive code is concentrated in a few hot spots—a version of the Pareto principle, also known as the “80/20” rule. Thus most of the code in a Python application doesn’t need to be performance-optimized, just a few critical pieces. You can incrementally translate those hot spots into Cython, and so get the performance gains you need where it matters most. The rest of the program can remain in Python for the convenience of the developers.

How to use Cython

Consider the following code, taken from Cython’s documentation:

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

 

This is a toy example, a not-very-efficient implementation of an integral function. As pure Python code, it’s slow, because Python must convert back and forth between machine-native numerical types and its own internal object types.

Now consider the Cython version of the same code, with Cython’s additions underscored:

cdef f(double x):
return x**2-x

def integrate_f(double a, double b, int N):
cdef int i
cdef double s, x, dx
s = 0
dx = (b-a)/N
for i in range(N):
s += f(a+i*dx)
return s * dx

If we explicitly declare the variable types, both for the function parameters and the variables used in the body of the function (double, int, etc.), Cython will translate all of this into C. We can also use the cdef keyword to define functions that are implemented primarily in C for additional speed, although those functions can only be called by other Cython functions and not by Python scripts.

Cython advantages

Aside from being able to speed up the code you’ve already written, Cython grants several other advantages:

Working with external C libraries can be faster

Python packages like NumPy wrap C libraries in Python interfaces to make them easy to work with. However, going back and forth between Python and C through those wrappers can slow things down. Cython lets you talk to the underlying libraries directly, without Python in the way. (C++ libraries are also supported.)

You can use both C and Python memory management

If you use Python objects, they’re memory-managed and garbage-collected the same as in regular Python. But if you want to create and manage your own C-level structures, and use malloc/free to work with them, you can do so. Just remember to clean up after yourself.

You can opt for safety or speed as needed

Cython automatically performs runtime checks for common problems that pop up in C, such as out-of-bounds access on an array, by way of decorators and compiler directives (e.g., @boundscheck(False)). Consequently, C code generated by Cython is much safer by default than hand-rolled C code.

If you’re confident you won’t need those checks at runtime, you can disable them for additional speed gains, either across an entire module or only on select functions.

Cython also allows you to natively access Python structures that use the “buffer protocol” for direct access to data stored in memory (without intermediate copying). Cython’s “memoryviews” let you work with those structures at high speed, and with the level of safety appropriate to the task.

Cython C code can benefit from releasing the GIL

Python’s Global Interpreter Lock, or GIL, synchronizes threads within the interpreter, protecting access to Python objects and managing contention for resources. But the GIL has been widely criticized as a stumbling block to a better-performing Python, especially on multicore systems.

If you have a section of code that makes no references to Python objects and performs a long-running operation, you can mark it with the with nogil: directive to allow it to run without the GIL. This frees up the Python interpeter to do other things, and allows Cython code to make use of multiple cores (with additional work).

Cython can use Python type hinting syntax

Python has a type-hinting syntax that is used mainly by linters and code checkers, rather than the CPython interpreter. Cython has its own custom syntax for code decorations, but with recent revisions of Cython you can use Python type-hinting syntax to provide type hints to Cython as well.

Cython limitations

Keep in mind that Cython isn’t a magic wand. It doesn’t automatically turn every instance of poky Python code into sizzling-fast C code. To make the most of Cython, you must use it wisely—and understand its limitations:

Little speedup for conventional Python code

When Cython encounteres Python code it can’t translate completely into C, it transforms that code into a series of C calls to Python’s internals. This amounts to taking Python’s interpreter out of the execution loop, which gives code a modest 15 to 20 percent speedup by default. Note that this is a best-case scenario; in some situations, you might see no performance improvement, or even a performance degradation.

Little speedup for native Python data structures

Python provides a slew of data structures—strings, lists, tuples, dictionaries, and so on. They’re hugely convenient for developers, and they come with their own automatic memory management. But they’re slower than pure C.

Cython lets you continue to use all of the Python data structures, although without much speedup. This is, again, because Cython simply calls the C APIs in the Python runtime that create and manipulate those objects. Thus Python data structures behave much like Cython-optimized Python code generally: You sometimes get a boost, but only a little.

Cython code runs fastest when “pure C”

If you have a function in C labeled with the cdef keyword, with all of its variables and inline function calls to other things that are pure C, it will run as fast as C can go. But if that function references any Python-native code, like a Python data structure or a call to an internal Python API, that call will be a performance bottleneck.

Fortunately, Cython provides a way to spot these bottlenecks: a source code report that shows at a glance which parts of your Cython app are pure C and which parts interact with Python. The better optimized the app, the less interaction there will be with Python.

IDG

A source code report generated for a Cython application. Areas in white are pure C; areas in yellow show interaction with Python’s internals. A well-optimized Cython program will have as little yellow as possible. The expanded last line shows the C code underyling its corresponding Cython code.

Cython NumPy

Cython improves the use of C-based third-party number-crunching libraries like NumPy. Because Cython code compiles to C, it can interact with those libraries directly, and take Python’s bottlenecks out of the loop.

But NumPy, in particular, works well with Cython. Cython has native support for specific constructions in NumPy and provides fast access to NumPy arrays. And the same familiar NumPy syntax you’d use in a conventional Python script can be used in Cython as-is.

However, if you want to create the closest possible bindings between Cython and NumPy, you need to further decorate the code with Cython’s custom syntax. The cimport statement, for instance, allows Cython code to see C-level constructs in libraries at compile time for the fastest possible bindings.

Since NumPy is so widely used, Cython supports NumPy “out of the box.” If you have NumPy installed, you can just state cimport numpy in your code, then add further decoration to use the exposed functions.

Cython profiling and performance

You get the best performance from any piece of code by profiling it and seeing firsthand where the bottlenecks are. Cython provides hooks for Python’s cProfile module, so you can use Python’s own profiling tools to see how your Cython code performs. No need to switch between toolsets; you can continue working in the Python world you know and love.

It helps to remember in all cases that Cython isn’t magic—that sensible real-world performance practices still apply. The less you shuttle back and forth between Python and Cython, the faster your app will run.

For instance, if you have a collection of objects you want to process in Cython, don’t iterate over it in Python and invoke a Cython function at each step. Pass the entire collection to your Cython module and iterate there. This technique is used often in libraries that manage data, so it’s a good model to emulate in your own code.

We use Python because it provides programmer convenience and enables fast development. Sometimes that programmer productivity comes at the cost of performance. With Cython, just a little extra effort can give you the best of both worlds.

If we explicitly declare the variable types, both for the function parameters and the variables used in the body of the function (double, int, etc.), Cython will translate all of this into C. We can also use the cdef keyword to define functions that are implemented primarily in C for additional speed, although those functions can only be called by other Cython functions and not by Python scripts.

Cython advantages

Aside from being able to speed up the code you’ve already written, Cython grants several other advantages:

Working with external C libraries can be faster

Python packages like NumPy wrap C libraries in Python interfaces to make them easy to work with. However, going back and forth between Python and C through those wrappers can slow things down. Cython lets you talk to the underlying libraries directly, without Python in the way. (C++ libraries are also supported.)

You can use both C and Python memory management

If you use Python objects, they’re memory-managed and garbage-collected the same as in regular Python. But if you want to create and manage your own C-level structures, and use malloc/free to work with them, you can do so. Just remember to clean up after yourself.

You can opt for safety or speed as needed

Cython automatically performs runtime checks for common problems that pop up in C, such as out-of-bounds access on an array, by way of decorators and compiler directives (e.g., @boundscheck(False)). Consequently, C code generated by Cython is much safer by default than hand-rolled C code.

If you’re confident you won’t need those checks at runtime, you can disable them for additional speed gains, either across an entire module or only on select functions.

Cython also allows you to natively access Python structures that use the “buffer protocol” for direct access to data stored in memory (without intermediate copying). Cython’s “memoryviews” let you work with those structures at high speed, and with the level of safety appropriate to the task.

Cython C code can benefit from releasing the GIL

Python’s Global Interpreter Lock, or GIL, synchronizes threads within the interpreter, protecting access to Python objects and managing contention for resources. But the GIL has been widely criticized as a stumbling block to a better-performing Python, especially on multicore systems.

If you have a section of code that makes no references to Python objects and performs a long-running operation, you can mark it with the with nogil: directive to allow it to run without the GIL. This frees up the Python interpeter to do other things, and allows Cython code to make use of multiple cores (with additional work).

Cython can use Python type hinting syntax

Python has a type-hinting syntax that is used mainly by linters and code checkers, rather than the CPython interpreter. Cython has its own custom syntax for code decorations, but with recent revisions of Cython you can use Python type-hinting syntax to provide type hints to Cython as well.

Cython limitations

Keep in mind that Cython isn’t a magic wand. It doesn’t automatically turn every instance of poky Python code into sizzling-fast C code. To make the most of Cython, you must use it wisely—and understand its limitations:

Little speedup for conventional Python code

When Cython encounteres Python code it can’t translate completely into C, it transforms that code into a series of C calls to Python’s internals. This amounts to taking Python’s interpreter out of the execution loop, which gives code a modest 15 to 20 percent speedup by default. Note that this is a best-case scenario; in some situations, you might see no performance improvement, or even a performance degradation.

Little speedup for native Python data structures

Python provides a slew of data structures—strings, lists, tuples, dictionaries, and so on. They’re hugely convenient for developers, and they come with their own automatic memory management. But they’re slower than pure C.

Cython lets you continue to use all of the Python data structures, although without much speedup. This is, again, because Cython simply calls the C APIs in the Python runtime that create and manipulate those objects. Thus Python data structures behave much like Cython-optimized Python code generally: You sometimes get a boost, but only a little.

Cython code runs fastest when “pure C”

If you have a function in C labeled with the cdef keyword, with all of its variables and inline function calls to other things that are pure C, it will run as fast as C can go. But if that function references any Python-native code, like a Python data structure or a call to an internal Python API, that call will be a performance bottleneck.

Fortunately, Cython provides a way to spot these bottlenecks: a source code report that shows at a glance which parts of your Cython app are pure C and which parts interact with Python. The better optimized the app, the less interaction there will be with Python.

IDG

A source code report generated for a Cython application. Areas in white are pure C; areas in yellow show interaction with Python’s internals. A well-optimized Cython program will have as little yellow as possible. The expanded last line shows the C code underyling its corresponding Cython code.

Cython NumPy

Cython improves the use of C-based third-party number-crunching libraries like NumPy. Because Cython code compiles to C, it can interact with those libraries directly, and take Python’s bottlenecks out of the loop.

But NumPy, in particular, works well with Cython. Cython has native support for specific constructions in NumPy and provides fast access to NumPy arrays. And the same familiar NumPy syntax you’d use in a conventional Python script can be used in Cython as-is.

However, if you want to create the closest possible bindings between Cython and NumPy, you need to further decorate the code with Cython’s custom syntax. The cimport statement, for instance, allows Cython code to see C-level constructs in libraries at compile time for the fastest possible bindings.

Since NumPy is so widely used, Cython supports NumPy “out of the box.” If you have NumPy installed, you can just state cimport numpy in your code, then add further decoration to use the exposed functions.

Cython profiling and performance

You get the best performance from any piece of code by profiling it and seeing firsthand where the bottlenecks are. Cython provides hooks for Python’s cProfile module, so you can use Python’s own profiling tools to see how your Cython code performs. No need to switch between toolsets; you can continue working in the Python world you know and love.

It helps to remember in all cases that Cython isn’t magic—that sensible real-world performance practices still apply. The less you shuttle back and forth between Python and Cython, the faster your app will run.

For instance, if you have a collection of objects you want to process in Cython, don’t iterate over it in Python and invoke a Cython function at each step. Pass the entire collection to your Cython module and iterate there. This technique is used often in libraries that manage data, so it’s a good model to emulate in your own code.

We use Python because it provides programmer convenience and enables fast development. Sometimes that programmer productivity comes at the cost of performance. With Cython, just a little extra effort can give you the best of both worlds.

This is a toy example, a not-very-efficient implementation of an integral function. As pure Python code, it’s slow, because Python must convert back and forth between machine-native numerical types and its own internal object types.

Now consider the Cython version of the same code, with Cython’s additions underscored:

cdef double f(double x):
    return x**2-x

def integrate_f(double a, double b, int N):
    cdef int i
    cdef double s, x, dx
    s = 0
    dx = (b-a)/N
    for i in range(N):
        s += f(a+i*dx)
    return s * dx

If we explicitly declare the variable types, both for the function parameters and the variables used in the body of the function (double, int, etc.), Cython will translate all of this into C. We can also use the cdef keyword to define functions that are implemented primarily in C for additional speed, although those functions can only be called by other Cython functions and not by Python scripts.

Cython advantages

Aside from being able to speed up the code you’ve already written, Cython grants several other advantages:

Working with external C libraries can be faster

Python packages like NumPy wrap C libraries in Python interfaces to make them easy to work with. However, going back and forth between Python and C through those wrappers can slow things down. Cython lets you talk to the underlying libraries directly, without Python in the way. (C++ libraries are also supported.)

You can use both C and Python memory management

If you use Python objects, they’re memory-managed and garbage-collected the same as in regular Python. But if you want to create and manage your own C-level structures, and use malloc/free to work with them, you can do so. Just remember to clean up after yourself.

You can opt for safety or speed as needed

Cython automatically performs runtime checks for common problems that pop up in C, such as out-of-bounds access on an array, by way of decorators and compiler directives (e.g., @boundscheck(False)). Consequently, C code generated by Cython is much safer by default than hand-rolled C code.

If you’re confident you won’t need those checks at runtime, you can disable them for additional speed gains, either across an entire module or only on select functions.

Cython also allows you to natively access Python structures that use the “buffer protocol” for direct access to data stored in memory (without intermediate copying). Cython’s “memoryviews” let you work with those structures at high speed, and with the level of safety appropriate to the task.

Cython C code can benefit from releasing the GIL

Python’s Global Interpreter Lock, or GIL, synchronizes threads within the interpreter, protecting access to Python objects and managing contention for resources. But the GIL has been widely criticized as a stumbling block to a better-performing Python, especially on multicore systems.

If you have a section of code that makes no references to Python objects and performs a long-running operation, you can mark it with the with nogil: directive to allow it to run without the GIL. This frees up the Python interpeter to do other things, and allows Cython code to make use of multiple cores (with additional work).

Cython can use Python type hinting syntax

Python has a type-hinting syntax that is used mainly by linters and code checkers, rather than the CPython interpreter. Cython has its own custom syntax for code decorations, but with recent revisions of Cython you can use Python type-hinting syntax to provide type hints to Cython as well.

Cython limitations

Keep in mind that Cython isn’t a magic wand. It doesn’t automatically turn every instance of poky Python code into sizzling-fast C code. To make the most of Cython, you must use it wisely—and understand its limitations:

Little speedup for conventional Python code

When Cython encounteres Python code it can’t translate completely into C, it transforms that code into a series of C calls to Python’s internals. This amounts to taking Python’s interpreter out of the execution loop, which gives code a modest 15 to 20 percent speedup by default. Note that this is a best-case scenario; in some situations, you might see no performance improvement, or even a performance degradation.

Little speedup for native Python data structures

Python provides a slew of data structures—strings, lists, tuples, dictionaries, and so on. They’re hugely convenient for developers, and they come with their own automatic memory management. But they’re slower than pure C.

Cython lets you continue to use all of the Python data structures, although without much speedup. This is, again, because Cython simply calls the C APIs in the Python runtime that create and manipulate those objects. Thus Python data structures behave much like Cython-optimized Python code generally: You sometimes get a boost, but only a little.

Cython code runs fastest when “pure C”

If you have a function in C labeled with the cdef keyword, with all of its variables and inline function calls to other things that are pure C, it will run as fast as C can go. But if that function references any Python-native code, like a Python data structure or a call to an internal Python API, that call will be a performance bottleneck.

Fortunately, Cython provides a way to spot these bottlenecks: a source code report that shows at a glance which parts of your Cython app are pure C and which parts interact with Python. The better optimized the app, the less interaction there will be with Python.

IDG
A source code report generated for a Cython application. Areas in white are pure C; areas in yellow show interaction with Python’s internals. A well-optimized Cython program will have as little yellow as possible. The expanded last line shows the C code underyling its corresponding Cython code.

Cython NumPy

Cython improves the use of C-based third-party number-crunching libraries like NumPy. Because Cython code compiles to C, it can interact with those libraries directly, and take Python’s bottlenecks out of the loop.

But NumPy, in particular, works well with Cython. Cython has native support for specific constructions in NumPy and provides fast access to NumPy arrays. And the same familiar NumPy syntax you’d use in a conventional Python script can be used in Cython as-is.

However, if you want to create the closest possible bindings between Cython and NumPy, you need to further decorate the code with Cython’s custom syntax. The cimport statement, for instance, allows Cython code to see C-level constructs in libraries at compile time for the fastest possible bindings.

Since NumPy is so widely used, Cython supports NumPy “out of the box.” If you have NumPy installed, you can just state cimport numpy in your code, then add further decoration to use the exposed functions.

Cython profiling and performance

You get the best performance from any piece of code by profiling it and seeing firsthand where the bottlenecks are. Cython provides hooks for Python’s cProfile module, so you can use Python’s own profiling tools to see how your Cython code performs. No need to switch between toolsets; you can continue working in the Python world you know and love.

It helps to remember in all cases that Cython isn’t magic—that sensible real-world performance practices still apply. The less you shuttle back and forth between Python and Cython, the faster your app will run.

For instance, if you have a collection of objects you want to process in Cython, don’t iterate over it in Python and invoke a Cython function at each step. Pass the entire collection to your Cython module and iterate there. This technique is used often in libraries that manage data, so it’s a good model to emulate in your own code.

We use Python because it provides programmer convenience and enables fast development. Sometimes that programmer productivity comes at the cost of performance. With Cython, just a little extra effort can give you the best of both worlds.

5/5 - (2 امتیاز)

راستی! برای دریافت مطالب جدید در کانال تلگرام یا پیج اینستاگرام سورس باران عضو شوید.

پکیج پیشرفته آموزش برنامه نویسی سی شارپ 2017 | مختص ورود به بازار کار + آموزش ساخت بازی Quiz of King
  • انتشار: ۲۶ دی ۱۳۹۷

دسته بندی موضوعات

آخرین محصولات فروشگاه

مشاهده همه

نظرات

بازخوردهای خود را برای ما ارسال کنید

این سایت از اکیسمت برای کاهش هرزنامه استفاده می کند. بیاموزید که چگونه اطلاعات دیدگاه های شما پردازش می‌شوند.