Cython vs Python vs PyPy vs CPython: Python with C and Beyond

QUICK SUMMARY ↬

The purpose of this article is to discuss various Python compilers like Cython, PyPy, IronPython and Nuitka that allow you to mix Python and C or C++/C# code. Developers typically mix Python and C together to utilize existing libraries and packages to execute Python at the speed of C.

7 min read
Updated: Oct 27, 2021

Python is among the most popular programming languages and is used heavily for machine learning, web development, artificial intelligence, data science, scientific computing, GUI development, games development, and much more.

Python was created by Guido Van Rossum in 1991 and its ecosystem has seen continuous growth including the rise in the number of Compilers like Cython, PyPy, and Nuitka. 

Before we dive deep into the Cython vs Python or Cython vs PyPy debate further, let us briefly look at why they exist in the first place?

Cython vs PyPy vs Python vs CPython: What to Expect?

In the current context, the purpose of a compiler is to transform a piece of code into another format, it can be converting a python program to a c program, to machine code, to intermediate bytecode, or something else. 

There are quite a few reasons that many compilers exist as of today, and the most prominent one is that Python being an interpreted language is slow out of the box.

The default compiler of Python is CPython which acts more like an interpreter, making overall python program execution slow.

To overcome this slowness, there are compilers like Cython that convert Python to c code which can be further compiled to executables. But this is not the only reason for the existence of Cython, we will see why.

Another good reason these compilers exist is that they allow you to mix Python with C, C++, and other languages like C# that fall under the .Net space. You might want to mix C, C++, or C# with Python to reuse existing libraries of Python and C, etc.

Note: If you need to write and interpret just Python code using an online tool then you can check out this article – Finding the best online python compiler.

What is CPython?

Let us start with CPython. CPython is the official implementation of Python programming specifications and comes as part of the official Python package. It is officially upgraded to cater to any improvements in Python specifications or to handle new releases of Python. So, CPython and Python specifications go hand in hand.

CPython compiles .py files to .pyc files in the background, .pyc files contain the python bytecode. Python bytecode is then interpreted by the Python Virtual Machine – PVM, which is required to execute Python.

CPython operates more like an interpreter and utilizes GIL (Global interpreter Lock) to ensure only one thread executes at a time. For all beginners, this is where you typically start off with your Python journey.

Cython – Python and C

cython

Cython is the compiler that understands both C as well as Python specifications, except for a few limitations. You can call it a superset of both Python and C.

From a Python developer perspective, it allows you to utilize existing C libraries in Python with the aim to convert it all to executables, using a standard C/C++ compiler. The mixed program is first converted to a C program and then compiled into the executables. You can use Cython to convert Python to C and then to exe or dll files, as required.

Why use Cython?
  • Allows you to use C style static type declarations in Python code
  • Provides Python and C debugger in one package
  • Develop performant applications using existing libraries like Keras, Matplotlib, and NumPy from a huge Python ecosystem.

The contribution of Cython to the Python ecosystem is huge since it allows developers to write Python modules in the form of c extensions. Developers do so with the simplicity of Python avoiding the much more complex C. Many popular libraries like SciPy and Pandas are written in Cython itself.

You may also like to read: Key Python Features.

Cython vs Python vs CPython: By now you must have understood the differences between the three. Python is just a programming language with standard specifications for developers to write programs. CPython is the default compiler/interpreter that understands the code written using python specifications. Cython goes a step beyond and understands C specifications as well along with Python and is capable to convert it all together to C code.

Cython

PyPy Python – Python JIT (Just in Time) Compiler

PyPy

PyPy is another good alternative to default CPython. PyPy uses Just in Time Compilation (JIT) and typically provides ~4x faster speed when compared to CPython. Python programs are typically memory hungry, PyPy also helps optimize memory usage at run time.

It is important to note that JIT compilers do not always provide speed and memory advantages. JIT itself needs to load in the memory and hence is an overhead in the first place.

Small and simple python programs might end up faster without PyPy. But when it comes to long-running and memory-heavy Python programs, the speed and memory advantage is significant.

Also, various scientific Python modules like scikit-learn utilize Python C API where PyPy does not provide any performance gains. You must look out for CFFI (C Foreign Function Interface for Python) repackaged versions of such libraries to get the performance advantage.

I hope this settles the Python vs PyPy debate as well and clarifies that PyPy is completely different from Cython and exists for a different purpose.

PyPy

Nuitka – Python and C

Nuitka

Nuitka is another Python compiler that aims to speed up Python performance. It is developed and maintained by Kay Hayen and slowly gaining popularity among the community of Python developers.

Like Cython, Nuitka is also a Python to c compiler, but it utilizes libpython for execution.

Kay Hayen is targeting to eliminate the use of libpython in favor of native c data types for the further execution speed boost.

Cython is gaining ground much faster, Nuitka really needs to bring in something disruptive to outsmart Cython. I will try out Nuitka for experimentation but would choose Cython for my production projects.

You can set up Nuitka on Linux, macOS, Windows, and a few other platforms, it is free to use under Apache License.

Nuitka

IronPython – C# (.Net) and Python

IronPython

IronPython brings Python programming to the .Net world. You can utilize the IronPython package to call Python code/libraries from C# programs.

From a Python developer perspective, you can include existing .Net and Python packages/libraries in your programs, and code the rest in Python or C#.

IronPython is written in C Sharp itself and was released back in 2006. It is free to use and works on all platforms that run .Net and Python, including Mac OS, Windows, and Linux.

IronPython

Boost Python: C++ Wrappers for Python

Boost Python provides an option to expose C++ functions, objects, and classes to Python and the other way round. You can wrap C++ without the need to change C++ code and use the wrappers in Python directly.

C++ becomes an ideal option for developers who want to utilize existing C++ libraries in Python instead of building it all from scratch.

On the other hand, C++ developers can utilize numeric libraries like NumPy in C++ projects by including the extensions provided by the Boost Python community.

Boost Python

RustPython – Rust, and Python (Beyond C ecosystem)

RustPython integrates Python with the Rust ecosystem. RustPython is the Python interpreter written in the Rust programming language and allows you to write python programs in the Rust environment.

Rust Python

You can then compile Python/Rust mixed programs to web assembly code to run in the browser.

At the time of writing, RustPython is under development and not ready for production use. Though it is gaining popularity and has a fast-growing community that is working on enabling support for the latest python specifications.

RustPython

Closing Thoughts

This brings us to the end of the Python vs Cython vs CPython vs others debate. All the compilers listed in this article focus on one of the below –

  • Improve execution speed of Python
  • provide the option to utilize existing code libraries of C, C++, and C# or vice versa
  • Or to use Python to do something beyond its capabilities.

Though there are other compilers that allow you to mix Python with other programming languages including JavaScript, the most used to date is Cython. It is the one that is making life easier for Python extension developers.

Tags

Team NF

@noeticforce

The team behind the noeticforce.com platform also loves to research and write, anything and everything technology.

More from Noeticforce
Join noeticforce

Create your free account to customize your reading & writing experience

Ⓒ 2021 noeticforce — All rights reserved