Saturday, February 06, 2010

Cython , probably the best programming language in the world.

1. Introduction

So I guess you have heard all those people saying that there is no such thing as the best programming language in the world, only the right tool for the job. And its true , each language has its advantages and disadvantages.

But Cython , makes me make an exception to this rule.

Why ?

Well because it combines the best a programming language has to offer leaving all the negatives out.

2. What is Cython ?

So what is Cython ?

Well Cython is Python. It will accept any Python code , even Python 3 , subject to some limitation because it is an ongoing project .

However Cython is more than python. The first thing making Cython different is that it converts python code to C code. This code can be compiled with any C compliant compiler and produce either an executable or dynamic linked library (Known as DLL on windows) .

ok so whats the big deal ?

Well this alone offers double the speed for python which is fine but not very impressive if you take a look to tools like ctypes an psyco.

The next cool thing is that it allows you to mix C code with python code, so if you happen to code in C it can be the perfect bridge between C and Python. Again fine but it can be done with other tools as well.

However what makes what makes Cython REALLY interesting is that it expands the Python syntax. It expands it in such way to implement C features. One of these features is statically typed variables.

The key word here is "expands" not "replaces" , which means that you can use dynamically typed variables with statically typed variables.

But why we need statically typed variables in python ?

Well the answer to this is speed. To put it simply Cython code taking advantage of statically typed variables, to increase speed from 2 to 1000 times. Yes you heard correctly there are benchmarks that show Cython run 1000 times faster than Python . Here a benchmark that sho ws Cython being faster than C !!!. And if you think that this requires any major recoding , then think again, it only requires putting the name of the type infront of your variable which is nothing more than on or two words.

Remember that Cython is Python, which means that all you write is 99% python you just add a couple of words here and there with no major editing.

Even the compilation process is automatic taking advantage of the onboard python distribution tools, requiring the usual setup.py script.

Cython Advantages

1) 100% Python syntax, making dead easy to use and code with. You can use any existing python code with no change to your code.

2) C speed, cause it supports many of the features of C language making it extremely faster alot faster than psyco and ctypes.There is some overhead because of the conversion but it usually quite small from 40% to 100%. However there have been benchmarks that have show Cython outperform C by 40% which is is not unusual. Usually for statically typed variable except a boost in speed from 50 to 1000 times compared to the same python code

3) you can use any C code any time, just import it and it becomes accessible to your Cython code. Excellent for wrapping C libraries

4) Cython now supports and C++.

5) You never need to mess with C code all happens from inside python syntax.

6) Converts the python code to C code. Well I do not know how useful that is as a few lines of python code turns to thousnads of lines of c code but for those who want to edit code to kingdom come it will be useful. Again I remind you that Cython does not force you to write any C code to take enjoy its advantages.

7) Cython can be compiled to dynamic link libraries that can be imported and used like any python library out there.

8) Has the support of Google , so it is actively developed.

Disadvantages

1) Cython is Python, meaning it will still require python installation and will run from inside a python VM as any python app. I dont know whether this is a disadvantage per se but for those that think that what to consider Cython as a complete replacement to C , must take this into consideration.

2) Its does not offer automatic wrapping for C libaries like Swig , so if you want to wrap Alot of C functions/ Libraries , then Cython is not the best tool for that job , but it may provide support for this in the future.

Sum Up


Cython offers C speed from inside python with a tiny amount of recoding, it can be uses . Bare in mind that it offers alot of C features not mentioned here so the best you can do is visit the website and read the excellent documentation .

Here is the Cython website.


Links

Links offering cython benchmarks and additional information





This one is a benchmark that show an almost 1000 times speed up over python but it uses pyrex ,bare in mind that Cython is based on pyrex so the same speed should apply. Also note the C speed it 2.15 seconds while Pyrex is 2.5 that is even less than 40% diference.



4 comments:

kebenaran said...

I would like to know how easy is it to make an executable using Cython if, for example, compared to VB6. This is always a very important issue for me. I would like to be able to make a program turn it into exe and e-mail it to somebody who can just double click it and use it. I know a lot of enthusiasts that gave up on a programming language because it took too long to see a practical stand alone application that can be created with that particular language.

kilon said...

Its possible. There is an --embed command which basically embeds the python runtime to the programm and creates a single executable. The good news is that it works not only for windows but for macos and linux as well. The bad news is that its not Cython's main focus to create standalones but to create extension to python programms. So I dont know how reliable this process is.

However it is possible and certainly doable. Cython uses exactly the same tools that Python uses to packages the application to executables. One of them is py2exe.

here is the how to

http://wiki.cython.org/EmbeddingCython

On the other hand installing python is pretty much a straightforward process, you download and install and only takes 10 seconds. If you are lucky enough not to use Windows then both macos and linux come already with python pre installed, so already python is executed in those OSs with a single click. Of course the same applies for JAva as well. It comes included in MACOS and Linux. Only Microsoft decides to stay behind in these matters. Oh well!!!

Ah I forgot to add , that I dont know what happens with ironpython, because its .NET language , it may be possible not to require python installed. Obviously .NET framework will be required to be installed, but if I am not wrong, recent windows version come with .NET framework pre installed. But you will have to do your own search for this.

Well I dont care about what enthusiasts do. I do not care also about users that are too lazy to waste 10 seconds of their life to install python only once just to run my app.

Unknown said...

Anytime I hear "... has C features" mentioned in the 'pro' column of a language, I'm instantly filled with dread. C is an awful, awful language.

Furthermore, having never tried Cython, I can't envision a scenario where mixing paradigms from two independently-developed languages will yield anything elegant, but I could be wrong.

That said, if you're looking for the statically-typed goodness with a lightweight speed and dynamic feel (not to mention Google supported), you should check out "Go". It's got a lot of C's speed, Python's Zen, and Ada's safety.

Kudos to you for acknowledging that there is something of an absolute scale on which programming languages can be compared. :) Your article was a good read!

kilon said...

Hello Craig I think you misread my post, I don't try to say that C is a elegant language. Just emphasize the fact that Cython is ideal in a sense that it unites C speed and beauty and power of python.

Cython is not a replacement neither to python or C. It serves a specific role. I do think however if you are concerned with speed and love python is best compromise.

I advice you to give it a try.

I dont think there is such a thing as best programming language , everything has its pros and cons. And to be sincere I have left python for smalltalk and lisp. But is certainly a very interesting choice.