Me Cython

When C met Python they made something beautiful...
and its called Cython.

Cython blends Python with C and C++, and what i have heard is its good, its fast. Python being the one being easy to understand but C being the one with optimized performance.

Alright, lets clear up one thing that i too had doubt earlier. Cython and CPython are totally different. CPython provides a C-level interface into the python language; the interface is known as the Python/C API. Cython uses this C interface extensively and therefore Cython depends on CPython. Hence Cython is not just an implementation of Python rather it needs CPython runtime .

Lets check the Python funtion:

def fibo(n):
   a = 0.0
   b = 1.0
   for i in range(n):
      a= a + b
      b=a
   return a

The above code is both valid in Python and Cython, but lets check out if we can somehow we are gonna improve its performance.
The C version
double cfibo(int n)
{
   int i;
   double a=0.0,b=1.0,tmp;
   for(i=0;i<n;++i)
   {   tmp=a;
       a=a+b;
       b=tmp;
   }
   return a;
}

Lets, blend them both.

def fiblo(int n):
   cdef int i
   cdef double a=0.0,b=1.0
   for i in range(n):
      a,b =a+b,a
   return a

Yes its true python takes a lot of time to run.And C is too Cheetah ...
but Cython is better than better than or say a lot better than Python yup not as perfect as C BUT YUP A LOT BETTER.

Comments

Popular Posts