<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nicholas Obert</title>
    <description>The latest articles on DEV Community by Nicholas Obert (@nic_obert).</description>
    <link>https://dev.to/nic_obert</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F851243%2Ff0ef1716-d4db-483e-8106-241d7609e081.jpeg</url>
      <title>DEV Community: Nicholas Obert</title>
      <link>https://dev.to/nic_obert</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nic_obert"/>
    <language>en</language>
    <item>
      <title>Speed Up Your Python Codebases With C Extensions</title>
      <dc:creator>Nicholas Obert</dc:creator>
      <pubDate>Sat, 23 Apr 2022 17:28:31 +0000</pubDate>
      <link>https://dev.to/nic_obert/speed-up-your-python-codebases-with-c-extensions-3j0g</link>
      <guid>https://dev.to/nic_obert/speed-up-your-python-codebases-with-c-extensions-3j0g</guid>
      <description>&lt;h3&gt;
  
  
  Bring the speed of C to your Python programs
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qgUFeyjZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/54ny2w90x8xyhpyjrs0e.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qgUFeyjZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/54ny2w90x8xyhpyjrs0e.jpg" alt="Photo by [PAUL SMITH](https://unsplash.com/@sumo?utm_source=medium&amp;amp;utm_medium=referral) on [Unsplash](https://unsplash.com/?utm_source=medium&amp;amp;utm_medium=referral)" width="700" height="466"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Python, being both easy and powerful, has become one of the most popular programming languages. Nonetheless, it sometimes lacks the much-valued speed of statically typed and precompiled programming languages like C and Java.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is Python Slow?
&lt;/h2&gt;

&lt;p&gt;As you may know, Python is generally implemented through an &lt;a href="https://g.co/kgs/eJyYmN"&gt;interpreter&lt;/a&gt;. This can cause the code execution to be rather slow compared to languages, like C and Java, with compiled implementations and whose source code is compiled in advance into &lt;a href="https://g.co/kgs/rAZgv2"&gt;machine&lt;/a&gt;/&lt;a href="https://g.co/kgs/WuXQ2o"&gt;byte&lt;/a&gt; code. However, this topic is beyond the scope of the article.&lt;/p&gt;



&lt;h2&gt;
  
  
  How Can You Speed Up Python Code?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9ErPvvwX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AsYcWbMDK22JLyXNIFgQ0FA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9ErPvvwX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AsYcWbMDK22JLyXNIFgQ0FA.png" alt="" width="598" height="265"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Unless you have to perform computationally heavy operations, Python’s speed is not usually a problem. This is where C extensions come in handy.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;C extensions are a way to code functions in C, compile them into a Python module and use them in your source code as a normal Python library.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Many popular modules are actually written in C or C++ (e.g. &lt;a href="https://numpy.org/"&gt;numpy&lt;/a&gt;, &lt;a href="https://pandas.pydata.org/"&gt;pandas&lt;/a&gt;, &lt;a href="https://www.tensorflow.org/"&gt;tensorflow&lt;/a&gt;…) for better performance and/or low-level functionalities.&lt;/p&gt;

&lt;p&gt;A few quick disclaimers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;C extensions do not work for Python implementations other than &lt;a href="https://en.wikipedia.org/wiki/CPython"&gt;Cpython&lt;/a&gt;. This should not be a problem since Cpython is the default one.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It is recommended you have basic knowledge of the C programming language. However, if you only know Python you should be able to follow along without any problems.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How to Build a C Extension
&lt;/h2&gt;

&lt;p&gt;As an example, let’s implement the classic &lt;code&gt;fib(n)&lt;/code&gt; function. &lt;code&gt;fib(n)&lt;/code&gt; takes in a number, &lt;code&gt;n&lt;/code&gt;, and returns the corresponding number in the Fibonacci sequence. Then we will compare the performance of the Python and the C version.&lt;/p&gt;

&lt;p&gt;First of all, you need the Python C API &lt;code&gt;Python.h&lt;/code&gt;. It’s a C &lt;a href="https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html#:~:text=A%20header%20file%20is%20a,Header%20files%20serve%20two%20purposes."&gt;header file&lt;/a&gt; that contains everything that’s needed to interface with Python.&lt;/p&gt;

&lt;h3&gt;
  
  
  Python API installation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;On Linux you usually have to install the &lt;code&gt;python-dev&lt;/code&gt; or &lt;code&gt;python3-dev&lt;/code&gt; package, if not already present. (Note that on some distros the package name may be different)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;By default, if installed with the default installer, Windows should come with Python.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;MacOs should also come with Python. If it does not, &lt;code&gt;brew reinstall python&lt;/code&gt; should do the trick.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, open the code editor of your choice and create the C module file. It should be named by convention — something along the lines of &lt;code&gt;module_name.c&lt;/code&gt;, although you can name it whatever you want. Here we’ll call it &lt;code&gt;c_module.c&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Before you start coding your extension, you need to include some core definitions and declarations.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
It is recommended that you put these lines at the beginning of your file for &lt;a href="https://docs.python.org/3/c-api/intro.html#:~:text=Since%20Python%20may%20define%20some%20pre-processor%20definitions%20which%20affect%20the%20standard%20headers%20on%20some%20systems%2C%20you%20must%20include%20Python.h%20before%20any%20standard%20headers%20are%20included."&gt;compatibility purposes&lt;/a&gt;.

&lt;p&gt;Since in Python everything is an object, our &lt;code&gt;c_fib(n)&lt;/code&gt; function should return one, precisely a &lt;code&gt;PyObject&lt;/code&gt; pointer (defined in &lt;code&gt;Python.h&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
Then it’s necessary to &lt;em&gt;declare which functions to export&lt;/em&gt; from the module to make them accessible to Python.&lt;br&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Defining the module methods
&lt;/h3&gt;

&lt;p&gt;Every exported method is represented as a struct comprised of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The exported method name (in this case "&lt;code&gt;c_fib&lt;/code&gt;”)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The actual method to be exported (&lt;code&gt;c_fib&lt;/code&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The type of arguments the method takes (in this case &lt;code&gt;METH_VARARGS&lt;/code&gt;). This is from the documentation on &lt;code&gt;METH_VARARGS&lt;/code&gt;: &lt;em&gt;“This is the typical calling convention, where the methods have the type &lt;code&gt;PyCFunction&lt;/code&gt;. The function expects two `PyObject&lt;/em&gt;&lt;code&gt; values. The first one is the &lt;/code&gt;self&lt;code&gt; object for methods; for module functions, it is the module object. The second parameter (often called &lt;/code&gt;args`) is a tuple object representing all arguments.”*&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A &lt;code&gt;const char*&lt;/code&gt; describing the method&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Defining the module
&lt;/h3&gt;

&lt;p&gt;The module is represented as a struct, as shown in the code above. It’s self-documenting, except for the &lt;a href="https://docs.python.org/3/c-api/module.html#:~:text=PyDoc_STRVAR%20is%20used.-,Py_ssize_t%20m_size,-%C2%B6"&gt;m_size&lt;/a&gt; argument, which we set to &lt;code&gt;-1&lt;/code&gt;. From the documentation:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Setting &lt;code&gt;m_size&lt;/code&gt; to &lt;code&gt;-1&lt;/code&gt; means that the module does not support sub-interpreters, because it has global state.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Module initialization function
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;PyMODINIT_FUNC&lt;/code&gt; gets called when the module is imported, initializing it. Note that the function name must start with &lt;code&gt;PyInit_&lt;/code&gt; and end with your module’s name, hence &lt;code&gt;PyInit_c_module()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;These are just a few of the Python API’s functionalities. For more information and features visit the API’s &lt;a href="https://docs.python.org/3/c-api/index.html"&gt;documentation page&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compile Your Extension to a Python Module
&lt;/h2&gt;

&lt;p&gt;Once the C code is complete, you have to compile it to a Python module. Luckily there are a bunch of built-in tools that allow you to do exactly that.&lt;/p&gt;

&lt;p&gt;Create a Python script, traditionally named &lt;code&gt;setup.py&lt;/code&gt;, and insert the following code:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
This script has many functionalities, but we’ll be using only the &lt;code&gt;build&lt;/code&gt; and &lt;code&gt;install&lt;/code&gt; commands. For more info, take a look at the &lt;a href="https://docs.python.org/3/distutils/setupscript.html"&gt;documentation&lt;/a&gt; or just run the script with the help flag:

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; python3 setup.py --help
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;From the command line run the following:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python3 setup.py build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;This will create a directory named &lt;code&gt;build&lt;/code&gt; and put the compiled libraries in it. When done, run this:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python3 setup.py install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;This will install the just-built libraries on your system, making them accessible from anywhere.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note that you might need root/admin privileges in order to do that. You don’t have to install it system-wide, but if you skip the install process you’ll have to use &lt;a href="https://realpython.com/absolute-vs-relative-python-imports/"&gt;relative imports&lt;/a&gt; in order to use the extension.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Use C Extensions Inside a Python Program
&lt;/h2&gt;

&lt;p&gt;Inside your Python file, import the newly created module using the name you chose. In our case, it is &lt;code&gt;c_module&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
As you can see, the extension can be used the same way as any other Python module would.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Does It Compare With the Plain Python Version?
&lt;/h2&gt;

&lt;p&gt;Now let’s compare the &lt;code&gt;c_fib&lt;/code&gt; function with its plain Python counterpart. We’ll use the built-in &lt;code&gt;time&lt;/code&gt; module:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
Output:

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: 5
py_res=5, py_time=5.245208740234375e-06
c_res=5, c_time=1.6689300537109375e-06
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;As expected, the C function is faster.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note that you might get different timings on a different machine, but the C version of the same code will always be faster.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let’s now try with some larger numbers:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: 10
py_res=55, py_time=3.147125244140625e-05
c_res=55, c_time=2.6226043701171875e-06

Input: 30
py_res=832040, py_time=0.40490126609802246
c_res=832040, c_time=0.004115581512451172

Input: 40
py_res=102334155, py_time=50.17047834396362
c_res=102334155, c_time=0.4414968490600586
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The C version clearly outperforms the Python one when it comes to large numbers. If you just have to do a few simple calculations, it might not be worth implementing them in C as the performance difference would be minimal. However, given a really time-consuming operation or a function that has to be repeated many times, Python’s speed might not be enough.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This is where C extensions really shine. You can leave all the heavy lifting to C, but still use Python as your main language.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Real-Life Use-Cases for C Extensions
&lt;/h2&gt;

&lt;p&gt;Say you have to perform some heavy calculations, be it for a cryptographic algorithm, deep learning model training, or processing large amounts of data. C extensions can relieve the burden from Python’s vertebrae and speed up your application.&lt;/p&gt;

&lt;p&gt;What if you instead wanted to build a low-level interface or work directly on memory from Python? C extensions are the way to go, given you know how to work with raw pointers.&lt;/p&gt;

&lt;p&gt;What about optimizing an already-existing Python application that performs badly but you don’t want to (or can’t) rewrite it in another language? C extensions are the answer.&lt;/p&gt;

&lt;p&gt;Or what if you’re just an optimization diehard who wants his code to run as fast as possible, but you still want some high-level abstractions for networking, a GUI, and so on. In this case, C extensions are definitely your best friend.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  &lt;em&gt;Time is the thing we always wish we had more of. Invest it wisely.&lt;/em&gt;
&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Whether you are a Python developer addicted to performance and efficiency, someone who likes mixing in different technologies, or you just want to experiment with something new, C extensions for Python are a great addition to your developer toolbox. Not only do they provide you with virtually free performance, but they can extend the functionalities of Python, saving it from the obsolete technologies stack.&lt;/p&gt;

&lt;p&gt;Thank you for reading.&lt;/p&gt;

</description>
      <category>python</category>
      <category>c</category>
      <category>performance</category>
      <category>cpp</category>
    </item>
  </channel>
</rss>
