Facts about Cython Programming Language


Cython is a programming language that is a superset of Python and can be compiled into C code for improved performance. Cython is developed to improve the execution speed and make the code faster compared to Python. In this article, we will look at some interesting facts about Cython Programming Language.

Cython can be used to speed up Python code

As the name suggests Cython is Python-like code that can be converted to C code for faster and improved execution speed. Developers can write code in Python which is then compiled into C code and makes the language much faster.

Example

In the below example, Cython code is nearly identical to the Python code, but the cpdef keyword tells Cython to compile the function to C code. This can result in a significant speedup, especially for computationally intensive tasks.

# Python code
def fibonacci(n):
   if n <= 1:
      return n
    return fibonacci(n-1) + fibonacci(n-2)
print(fibonacci(3))

# Cython code
cpdef int fibonacci(int n):
   if n <= 1:
      return n
    return fibonacci(n-1) + fibonacci(n-2)
print(fibonacci(3))

Output

2
2

Cython supports static typing.

Cython supports static typing which means the variable type should be declared at compile time, unlike Python in which the variable's type can change at runtime. Static typing leads to faster code execution.

Example

In the below example, the Python code does not specify the type of variables a and b whereas in Python code the variables a and b are already initialized with the integer data type.

# Python code
def add(a, b):
   return a + b
print(add(2,3))

# Cython code with static typing
cpdef int add(int a, int b):
    return a + b
print(add(2,3))

Output

5
5

Cython can interact with C code

Cython is designed to be compatible with C code, making it easy to integrate with existing C libraries. Cython code can be compiled into C code, which can then be linked with C code to create a single executable.

Example

In the below example, the hello_world() function is written in C code and declared as an external function in the Cython code. The greet() function written calls the hellow_world() function to print “Hellow_world”.

# C code
#include <stdio.h>

void hello_world() {
   printf("Hello, world!\n");
}

# Cython code
cdef extern void hello_world()

def greet():
   hello_world()

Output

Hello, world!

Cython can be compiled into C code

Example

The below code is written in Cython where the variables are declared as integer data type and also the return type is an integer. The below code can be compiled into C code using cythonize utility from cython package.

# Python code
def add_numbers(a, b):
   return a + b
print(add(2,3))

# Cython code with static typing
cpdef int add_numbers(int a, int b):
   return a + b
print(add(2,3))

Output

5
5
# setup.py file for compiling Cython code
from distutils.core import setup
from Cython.Build import cythonize

setup(
   ext_modules = cythonize("example.pyx")
)

After running python setup.py build_ext --in place, a compiled C module will be generated alongside the original Cython code.

# Example output (after compiling and importing the Cython module)
>>> from example import add_numbers
>>> add_numbers(2, 3)

Output

5

Conclusion

In this article, we discussed some facts about Cython Programming Language. Cython programming language is a Python-like language that is compiled into C code for faster execution speed. We explained how Cython can be used to speedup python code and how it interacts with C code. Cython is compiled to C code for faster execution.

Updated on: 10-Jul-2023

98 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements