TNS
VOXPOP
Do You Resent AI?
If you’re a developer, do you resent generative AI’s ability to write code?
Yes, because I spent a lot of time learning how to code.
0%
Yes, because I fear that employers will replace me and/or my peers with it.
0%
Yes, because too much investment is going to AI at the expense of other needs.
0%
No, because it makes too many programming mistakes.
0%
No, because it can’t replace what I do.
0%
No, because it is a tool that will help me be more productive.
0%
No, I am a highly evolved being and resent nothing.
0%
I don’t think much about AI.
0%
Python

Cython Offers the Ease of Python, the Speed of C++

Cython, a superset of Python, bridges the gap between Python and C or C++. Its aim is to make writing C extensions for Python as easy as Python itself.
Aug 6th, 2023 8:00am by
Featued image for: Cython Offers the Ease of Python, the Speed of C++
Image by Michael Reichelt from Pixabay.

Python is the preferred programming language for working with massive data sets, making it the go-to choice for machine learning, artificial intelligence, and statistical analysis.

But it’s not without flaws, with speed being one of its main weaknesses and another its inability to interact with hardware. C, on the other hand, is faster and can interact with hardware, but has a steep learning curve.

Cython, a superset of Python, bridges the gap between Python and C or C++. Its aim is to make writing C extensions for Python as easy as Python itself. The rationale is that the C extensions can perform much more quickly as stand-alone modules than those run through the Python interpreter.

Cython developers released Cython 3.0 earlier this month with some noteworthy improvements.

This recent blog post written by Mike James did a great job of covering the basics of the latest release of Cython. Cython expanded the use of pure Python mode, strengthened its NumPy compatibility, and made internal updates to enhance future compatibility with Python.

Is Python Getting too Complicated for New Users?
Efforts like the Cython project and the removal of the Global Interpreter Lock aim to gear Python for faster, high-throughput data analysis, but also introduce additional complexity.
No way: The improvements are much needed, backward compatible and optional to use.
0%
Yes: Python’s chief value is its simplicity, not its speed. Production can be executed with R, or C++ or some other performant language.
0%
Who cares? Python is too problematic even for new users (blank spaces, runaway libraries, etc.). Newbies, start with JavaScript instead.
0%
Cowboy Neal (h/t, Slashdot and Jack Kerouac)
0%

The Basics

Cython is an optimizing static compiler for both the Python programming language and the extended Cython programming language (based on Pyrex, a Python-like language for rapidly and easily writing Python extension modules). It provides developers the ability to write Python code that calls to and from C or C++ natively.

By using Cython, developers can turn readable Python code into plain C performance by adding static type declaration. By adding these efficiencies, Cython helps Python interact more efficiently with large data sets. Cython integrates natively with existing code and data from legacy, low-level or high-performance applications and libraries.

Cython 3.0

Recently, version 3 of Cython has been released. The list below is a non-exhaustive highlight of Cython’s new upgrades.

Expanded Pure Python Mode

Historically Cython used its own syntax, a combination of Python and the C-type declaration. This created challenges on its own by limiting the developer’s abilities to troubleshoot and debug with Python tooling as it doesn’t understand Cython’s syntax. As a solution, Cython developers created “pure Python mode.”

Pure Python mode is an alternative syntax that’s fully compatible with Python’s syntax. This meant developers could use their existing linting and code analysis tools on Cython code. The new expanded pure Python mode means the vast majority of Cython functions are now exposed in pure Python mode, including functions for calling external C libraries.

Deeper NumPy Compatibility

NumPy is a widely used Python library that focuses on scientific computing. NumPy creates a multidimensional array object, various derived objects, and an assortment of routines centered around performing quick operations on arrays. Developers can now write NumPy ufuncs directly in Cython. A simple numerical function written in Cython can be quickly and easily applied to the entire contents of a NumPy data structure. Though Cython and NumPy were always compatible, this new feature adds speed and more ease to development.

Internal Changes

Now Cython’s build is more compatible with ongoing updates to Python’s internal changes. Python has a new “limited API” that exposes a guaranteed stable subset of Python’s APIs, specifically for the type of tasks Cython does to hook into the Python interpreter. Cython has the initial, with growing support, for the limited API. This means Cython extensions built for one version of Python will also work in future versions of Python without needing to be recompiled.

Final Thoughts

As someone who doesn’t work with Python often, I find it interesting that this is the third article in as many months that I’ve written about tools that deepen the connection between Python and C. It’s such a marker as to where the industry is going. Data sets get larger and larger, Python remains the go-to, and now these tools are either popping up or getting better to further support the growth.

Group Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.