Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Noob-Ready Cython Tutorials [closed]

Tags:

I know a bunch of scripting languages, (python, ruby, lua, php) but I don't know any compiled languages like C/C++ , I wanted to try and speed up some python code using cython, which is essentially a python -> C compiler, aimed at creating C extensions for python. Basically you code in a stricter version of python which compiles into C -> native code.

here's the problem, I don't know C, yet the cython documentation is aimed at people who obviously already know C (nothing is explained, only presented), and is of no help to me, I need to know if there are any good cython tutorials aimed at python programmers, or if I'm gonna have to learn C before I learn Cython.

bear in mind I'm a competent python programmer, i would much rather learn cython from the perspective of the language I'm already good at, rather than learn a whole new language in order to learn cython.

1) PLEASE don't recommend psyco

edit: ANY information that will help understand the oficial cython docs is useful information

like image 823
spearfire Avatar asked Oct 17 '09 12:10

spearfire


People also ask

Should you learn Cython?

Since Python is widely used in the world of data science and only growing in its popularity, it is an important tool for anyone getting started in data science. Learning Python as part of a wider data analytics skill set will make you a stronger candidate, as data analysts are expected to have a wide array of skills.

What is Cython tutorial?

Welcome to a Cython tutorial. The purpose of Cython is to act as an intermediary between Python and C/C++. At its heart, Cython is a superset of the Python language, which allows you to add typing information and class attributes that can then be translated to C code and to C-Extensions for Python.

Does Cython compile to C?

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.


1 Answers

cython is good at two different things

  1. Interfacing with C language libraries
  2. Speeding up Python code

It probably gets more exposure from 1. hence the emphasis on the tutorial materials you've found towards C stuff. It sounds like you want to use it like 2. though.

From my experience with cython you can just try compiling your python programs and see if it works. It will get a bit faster (maybe). To get a lot faster you need to selectively turn python types into C types. This starts to bring out the power of cython.

If you look at the official tutorial you need to study where they've used the cdef keyword.

So to recap

  1. Make your existing python program compile with cython with as few changes as possible
  2. Declare some variables as cdef and make it work again
  3. If not fast enough go to step 2.

I'm sorry that isn't a pointer to a tutorial, but it should give you a direction to go in!

like image 59
Nick Craig-Wood Avatar answered Jan 03 '23 00:01

Nick Craig-Wood