{"id":50,"date":"2023-03-31T04:06:00","date_gmt":"2023-03-31T04:06:00","guid":{"rendered":"https:\/\/www.w3computing.com\/articles\/?p=50"},"modified":"2023-08-23T16:22:35","modified_gmt":"2023-08-23T16:22:35","slug":"optimizing-python-code-using-cython-and-numba","status":"publish","type":"post","link":"https:\/\/www.w3computing.com\/articles\/optimizing-python-code-using-cython-and-numba\/","title":{"rendered":"Optimizing Python code using Cython and Numba"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Python, being a high-level and user-friendly language, has become a popular choice among experienced developers for a wide range of applications. However, one common concern with Python is its performance. For computationally intensive tasks, Python&#8217;s execution speed can sometimes be a limiting factor. That&#8217;s where Cython and Numba come in \u2013 two powerful tools that can optimize your Python code and significantly boost its performance.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, we will discuss how you can optimize your Python code using Cython and Numba, both designed specifically for experienced developers. We&#8217;ll explore the basics, benefits, and best practices of each tool while providing examples to illustrate their usage. So, get ready to supercharge your Python code and make it faster than ever!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding Cython<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Cython is a superset of Python that allows developers to write Python-like code with optional static type declarations. The Cython compiler then translates this code into highly optimized C or C++ code, which is subsequently compiled into a Python extension module. This process can result in significant performance improvements, especially when dealing with computationally intensive tasks.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Installing Cython<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To get started with Cython, you can install it using pip:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">pip install cython\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Writing Cython Code<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Cython code is typically written in files with the &#8220;.pyx&#8221; extension. Here&#8217;s a simple example of a Cython function that calculates the sum of squares of two integers:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># sum_of_squares.pyx<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">sum_of_squares<\/span><span class=\"hljs-params\">(int a, int b)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> a * a + b * b<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Compiling Cython Code<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To compile the Cython code, you need to create a setup.py file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># setup.py<\/span>\n<span class=\"hljs-keyword\">from<\/span> setuptools <span class=\"hljs-keyword\">import<\/span> setup\n<span class=\"hljs-keyword\">from<\/span> Cython.Build <span class=\"hljs-keyword\">import<\/span> cythonize\n\nsetup(\n    ext_modules=cythonize(<span class=\"hljs-string\">\"sum_of_squares.pyx\"<\/span>)\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Then, compile the Cython module using the following command:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">python setup.py build_ext --inplace\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">This will generate a compiled Python extension module in your project directory, which can be imported and used just like any other Python module.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Cython Best Practices<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When optimizing your Python code with Cython, keep the following best practices in mind:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Use static typing<\/strong>: While not mandatory, declaring variable types can significantly improve the performance of your Cython code.<\/li>\n\n\n\n<li><strong>Profile your code<\/strong>: Use Python&#8217;s built-in profiling tools to identify performance bottlenecks before optimizing with Cython.<\/li>\n\n\n\n<li><strong>Use Cython-specific features<\/strong>: Leverage Cython&#8217;s unique features, such as memory views and typed memoryviews, for efficient memory access and manipulation.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Numba: Just-In-Time Compilation for Python<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Numba is an open-source just-in-time (JIT) compiler for Python that translates a subset of Python and NumPy code into machine code at runtime. It uses LLVM to compile the code, which can lead to significant performance improvements, particularly for numerical computations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Installing Numba<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To install Numba, use pip:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">pip install numba\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Using Numba<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Numba is easy to use \u2013 simply import it and apply the <code>@jit<\/code> decorator to the functions you want to optimize. Here&#8217;s an example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> numba <span class=\"hljs-keyword\">import<\/span> jit\n\n<span class=\"hljs-meta\">@jit<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">sum_of_squares<\/span><span class=\"hljs-params\">(a, b)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> a * a + b * b\n\n<span class=\"hljs-comment\"># Now, the sum_of_squares function will be compiled with Numba when called.<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Numba Best Practices<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To make the most of Numba, consider the following best practices:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Use Numba with NumPy<\/strong>: Numba is designed to work seamlessly with NumPy, and leveraging NumPy&#8217;s array-based computations can lead to further performance gains.<\/li>\n\n\n\n<li><strong>Utilize nopython mode<\/strong>: The <code>@jit<\/code> decorator has two modes &#8211; nopython and object. Nopython mode (<code>@jit(nopython=True<\/code>)) offers the best performance, as it avoids using the Python C API.<\/li>\n\n\n\n<li><strong>Parallelize your code<\/strong>: Numba offers support for parallelization using the <code>@njit<\/code> decorator (an alias for <code>@jit(nopython=True<\/code>)). By adding the <code>parallel=True<\/code> argument, you can automatically parallelize supported operations.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Comparing Cython and Numba<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Cython and Numba are both powerful tools for optimizing Python code, but they serve different purposes and come with their own set of advantages and limitations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Cython Pros and Cons<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Pros<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Can generate highly optimized C\/C++ code.<\/li>\n\n\n\n<li>Supports the entire Python language.<\/li>\n\n\n\n<li>Allows for fine-grained control over memory management.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Cons<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Requires a compilation step, which can be time-consuming.<\/li>\n\n\n\n<li>Learning curve for developers who are unfamiliar with C\/C++.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Numba Pros and Cons<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Pros<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Just-in-time compilation offers immediate performance improvements.<\/li>\n\n\n\n<li>Easy to integrate with existing Python and NumPy code.<\/li>\n\n\n\n<li>Automatic parallelization support.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Cons<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Limited to a subset of Python and NumPy features.<\/li>\n\n\n\n<li>May not offer the same level of optimization as Cython for non-numeric code.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Combining Cython and Numba<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In some cases, it might be beneficial to combine Cython and Numba to optimize different parts of your code. For example, you could use Cython to optimize Python-heavy code with complex data structures, while employing Numba for numerically intensive computations with NumPy arrays.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s a simple example that demonstrates the combined use of Cython and Numba:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># my_module.pyx<\/span>\ncimport numpy <span class=\"hljs-keyword\">as<\/span> cnp\n<span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n<span class=\"hljs-keyword\">from<\/span> numba <span class=\"hljs-keyword\">import<\/span> njit\n\n<span class=\"hljs-comment\"># Cython-optimized function<\/span>\ncpdef cnp.ndarray&#91;cnp.float64_t, ndim=<span class=\"hljs-number\">1<\/span>] cython_func(cnp.ndarray&#91;cnp.float64_t, ndim=<span class=\"hljs-number\">1<\/span>] arr):\n    cdef int i\n    cdef int n = arr.shape&#91;<span class=\"hljs-number\">0<\/span>]\n    cdef cnp.ndarray&#91;cnp.float64_t, ndim=<span class=\"hljs-number\">1<\/span>] result = np.empty(n, dtype=np.float64)\n\n    <span class=\"hljs-keyword\">for<\/span> i <span class=\"hljs-keyword\">in<\/span> range(n):\n        result&#91;i] = arr&#91;i] * arr&#91;i]\n\n    <span class=\"hljs-keyword\">return<\/span> result\n\n<span class=\"hljs-comment\"># Numba-optimized function<\/span>\n<span class=\"hljs-meta\">@njit<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">numba_func<\/span><span class=\"hljs-params\">(arr)<\/span>:<\/span>\n    result = np.empty_like(arr)\n    \n    <span class=\"hljs-keyword\">for<\/span> i <span class=\"hljs-keyword\">in<\/span> range(arr.shape&#91;<span class=\"hljs-number\">0<\/span>]):\n        result&#91;i] = arr&#91;i] * arr&#91;i]\n\n    <span class=\"hljs-keyword\">return<\/span> result<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">A Real-World Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this real-world example, we&#8217;ll implement a function to calculate the pairwise Euclidean distance between points in a two-dimensional space. The input is a NumPy array, where each row represents a point (x, y). We will optimize this function using both Cython and Numba.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1. The original Python function:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">euclidean_distance_python<\/span><span class=\"hljs-params\">(points)<\/span>:<\/span>\n    num_points = points.shape&#91;<span class=\"hljs-number\">0<\/span>]\n    distance_matrix = np.zeros((num_points, num_points))\n\n    <span class=\"hljs-keyword\">for<\/span> i <span class=\"hljs-keyword\">in<\/span> range(num_points):\n        <span class=\"hljs-keyword\">for<\/span> j <span class=\"hljs-keyword\">in<\/span> range(num_points):\n            x_diff = points&#91;i, <span class=\"hljs-number\">0<\/span>] - points&#91;j, <span class=\"hljs-number\">0<\/span>]\n            y_diff = points&#91;i, <span class=\"hljs-number\">1<\/span>] - points&#91;j, <span class=\"hljs-number\">1<\/span>]\n            distance_matrix&#91;i, j] = np.sqrt(x_diff**<span class=\"hljs-number\">2<\/span> + y_diff**<span class=\"hljs-number\">2<\/span>)\n\n    <span class=\"hljs-keyword\">return<\/span> distance_matrix<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>2. Optimizing with Cython:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create a file named <code>euclidean_distance_cython.pyx<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># euclidean_distance_cython.pyx<\/span>\n<span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\ncimport numpy <span class=\"hljs-keyword\">as<\/span> cnp\n\ncpdef cnp.ndarray&#91;cnp.float64_t, ndim=<span class=\"hljs-number\">2<\/span>] euclidean_distance_cython(cnp.ndarray&#91;cnp.float64_t, ndim=<span class=\"hljs-number\">2<\/span>] points):\n    cdef int num_points = points.shape&#91;<span class=\"hljs-number\">0<\/span>]\n    cdef cnp.ndarray&#91;cnp.float64_t, ndim=<span class=\"hljs-number\">2<\/span>] distance_matrix = np.zeros((num_points, num_points), dtype=np.float64)\n    cdef int i, j\n    cdef double x_diff, y_diff\n\n    <span class=\"hljs-keyword\">for<\/span> i <span class=\"hljs-keyword\">in<\/span> range(num_points):\n        <span class=\"hljs-keyword\">for<\/span> j <span class=\"hljs-keyword\">in<\/span> range(num_points):\n            x_diff = points&#91;i, <span class=\"hljs-number\">0<\/span>] - points&#91;j, <span class=\"hljs-number\">0<\/span>]\n            y_diff = points&#91;i, <span class=\"hljs-number\">1<\/span>] - points&#91;j, <span class=\"hljs-number\">1<\/span>]\n            distance_matrix&#91;i, j] = np.sqrt(x_diff * x_diff + y_diff * y_diff)\n\n    <span class=\"hljs-keyword\">return<\/span> distance_matrix<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Create a <code>setup.py<\/code> file to compile the Cython code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># setup.py<\/span>\n<span class=\"hljs-keyword\">from<\/span> setuptools <span class=\"hljs-keyword\">import<\/span> setup\n<span class=\"hljs-keyword\">from<\/span> Cython.Build <span class=\"hljs-keyword\">import<\/span> cythonize\n\nsetup(\n    ext_modules=cythonize(<span class=\"hljs-string\">\"euclidean_distance_cython.pyx\"<\/span>)\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Compile the Cython code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">python setup.py build_ext --inplace\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>3. Optimizing with Numba:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n<span class=\"hljs-keyword\">from<\/span> numba <span class=\"hljs-keyword\">import<\/span> njit\n\n<span class=\"hljs-meta\">@njit<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">euclidean_distance_numba<\/span><span class=\"hljs-params\">(points)<\/span>:<\/span>\n    num_points = points.shape&#91;<span class=\"hljs-number\">0<\/span>]\n    distance_matrix = np.zeros((num_points, num_points))\n\n    <span class=\"hljs-keyword\">for<\/span> i <span class=\"hljs-keyword\">in<\/span> range(num_points):\n        <span class=\"hljs-keyword\">for<\/span> j <span class=\"hljs-keyword\">in<\/span> range(num_points):\n            x_diff = points&#91;i, <span class=\"hljs-number\">0<\/span>] - points&#91;j, <span class=\"hljs-number\">0<\/span>]\n            y_diff = points&#91;i, <span class=\"hljs-number\">1<\/span>] - points&#91;j, <span class=\"hljs-number\">1<\/span>]\n            distance_matrix&#91;i, j] = np.sqrt(x_diff**<span class=\"hljs-number\">2<\/span> + y_diff**<span class=\"hljs-number\">2<\/span>)\n\n    <span class=\"hljs-keyword\">return<\/span> distance_matrix<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>4. Testing and comparing the performance:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n<span class=\"hljs-keyword\">from<\/span> euclidean_distance_cython <span class=\"hljs-keyword\">import<\/span> euclidean_distance_cython\n<span class=\"hljs-keyword\">from<\/span> euclidean_distance_numba <span class=\"hljs-keyword\">import<\/span> euclidean_distance_numba\n\nnum_points = <span class=\"hljs-number\">1000<\/span>\npoints = np.random.random((num_points, <span class=\"hljs-number\">2<\/span>))\n\n<span class=\"hljs-comment\"># Test the original Python function<\/span>\n%timeit euclidean_distance_python(points)\n\n<span class=\"hljs-comment\"># Test the Cython optimized function<\/span>\n%timeit euclidean_distance_cython(points)\n\n<span class=\"hljs-comment\"># Test the Numba optimized function<\/span>\n%timeit euclidean_distance_numba(points)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">You should observe that both the Cython and Numba optimized functions run significantly faster than the original Python function. The exact performance gains will depend on the hardware and software configurations, but in general, you can expect a substantial improvement in execution time. By applying Cython and Numba optimizations to real-world examples like this one, you can enhance the performance of your Python code and make it more suitable for computationally intensive tasks.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Keep in mind that the performance gains may vary depending on the specific use case and the nature of the code. It&#8217;s always a good idea to profile your code and test the optimizations to ensure that they deliver the desired improvements in your particular situation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In summary, Cython and Numba offer powerful optimization capabilities for experienced Python developers looking to improve the performance of their code. By understanding the use cases, best practices, and limitations of each tool, you can make informed decisions about when and how to apply these optimizations, ultimately leading to faster, more efficient Python code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python, being a high-level and user-friendly language, has become a popular choice among experienced developers for a wide range of applications. However, one common concern with Python is its performance. For computationally intensive tasks, Python&#8217;s execution speed can sometimes be a limiting factor. That&#8217;s where Cython and Numba come in \u2013 two powerful tools that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[4,6],"tags":[],"class_list":["post-50","post","type-post","status-publish","format-standard","category-programming-languages","category-python","entry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Optimizing Python code using Cython and Numba<\/title>\n<meta name=\"description\" content=\"Unlock the full potential of your Python code by leveraging the power of Cython and Numba for performance gains.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.w3computing.com\/articles\/optimizing-python-code-using-cython-and-numba\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Optimizing Python code using Cython and Numba\" \/>\n<meta property=\"og:description\" content=\"Unlock the full potential of your Python code by leveraging the power of Cython and Numba for performance gains.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.w3computing.com\/articles\/optimizing-python-code-using-cython-and-numba\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-03-31T04:06:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-23T16:22:35+00:00\" \/>\n<meta name=\"author\" content=\"w3compadmin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"w3compadmin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/optimizing-python-code-using-cython-and-numba\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/optimizing-python-code-using-cython-and-numba\\\/\"},\"author\":{\"name\":\"w3compadmin\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\"},\"headline\":\"Optimizing Python code using Cython and Numba\",\"datePublished\":\"2023-03-31T04:06:00+00:00\",\"dateModified\":\"2023-08-23T16:22:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/optimizing-python-code-using-cython-and-numba\\\/\"},\"wordCount\":953,\"commentCount\":0,\"articleSection\":[\"Programming Languages\",\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/optimizing-python-code-using-cython-and-numba\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/optimizing-python-code-using-cython-and-numba\\\/\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/optimizing-python-code-using-cython-and-numba\\\/\",\"name\":\"Optimizing Python code using Cython and Numba\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#website\"},\"datePublished\":\"2023-03-31T04:06:00+00:00\",\"dateModified\":\"2023-08-23T16:22:35+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\"},\"description\":\"Unlock the full potential of your Python code by leveraging the power of Cython and Numba for performance gains.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/optimizing-python-code-using-cython-and-numba\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/optimizing-python-code-using-cython-and-numba\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/optimizing-python-code-using-cython-and-numba\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Articles Home\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Programming Languages\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/programming-languages\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/programming-languages\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Optimizing Python code using Cython and Numba\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#website\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/\",\"name\":\"Developer Articles Hub\",\"description\":\"\",\"alternateName\":\"Developer Articles\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\",\"name\":\"w3compadmin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1781352167\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1781352167\",\"contentUrl\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1781352167\",\"caption\":\"w3compadmin\"},\"sameAs\":[\"http:\\\/\\\/w3computing.com\\\/articles\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Optimizing Python code using Cython and Numba","description":"Unlock the full potential of your Python code by leveraging the power of Cython and Numba for performance gains.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.w3computing.com\/articles\/optimizing-python-code-using-cython-and-numba\/","og_locale":"en_US","og_type":"article","og_title":"Optimizing Python code using Cython and Numba","og_description":"Unlock the full potential of your Python code by leveraging the power of Cython and Numba for performance gains.","og_url":"https:\/\/www.w3computing.com\/articles\/optimizing-python-code-using-cython-and-numba\/","article_published_time":"2023-03-31T04:06:00+00:00","article_modified_time":"2023-08-23T16:22:35+00:00","author":"w3compadmin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"w3compadmin","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/www.w3computing.com\/articles\/optimizing-python-code-using-cython-and-numba\/#article","isPartOf":{"@id":"https:\/\/www.w3computing.com\/articles\/optimizing-python-code-using-cython-and-numba\/"},"author":{"name":"w3compadmin","@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561"},"headline":"Optimizing Python code using Cython and Numba","datePublished":"2023-03-31T04:06:00+00:00","dateModified":"2023-08-23T16:22:35+00:00","mainEntityOfPage":{"@id":"https:\/\/www.w3computing.com\/articles\/optimizing-python-code-using-cython-and-numba\/"},"wordCount":953,"commentCount":0,"articleSection":["Programming Languages","Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.w3computing.com\/articles\/optimizing-python-code-using-cython-and-numba\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.w3computing.com\/articles\/optimizing-python-code-using-cython-and-numba\/","url":"https:\/\/www.w3computing.com\/articles\/optimizing-python-code-using-cython-and-numba\/","name":"Optimizing Python code using Cython and Numba","isPartOf":{"@id":"https:\/\/www.w3computing.com\/articles\/#website"},"datePublished":"2023-03-31T04:06:00+00:00","dateModified":"2023-08-23T16:22:35+00:00","author":{"@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561"},"description":"Unlock the full potential of your Python code by leveraging the power of Cython and Numba for performance gains.","breadcrumb":{"@id":"https:\/\/www.w3computing.com\/articles\/optimizing-python-code-using-cython-and-numba\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.w3computing.com\/articles\/optimizing-python-code-using-cython-and-numba\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.w3computing.com\/articles\/optimizing-python-code-using-cython-and-numba\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Articles Home","item":"https:\/\/www.w3computing.com\/articles\/"},{"@type":"ListItem","position":2,"name":"Programming Languages","item":"https:\/\/www.w3computing.com\/articles\/programming-languages\/"},{"@type":"ListItem","position":3,"name":"Python","item":"https:\/\/www.w3computing.com\/articles\/programming-languages\/python\/"},{"@type":"ListItem","position":4,"name":"Optimizing Python code using Cython and Numba"}]},{"@type":"WebSite","@id":"https:\/\/www.w3computing.com\/articles\/#website","url":"https:\/\/www.w3computing.com\/articles\/","name":"Developer Articles Hub","description":"","alternateName":"Developer Articles","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.w3computing.com\/articles\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561","name":"w3compadmin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1781352167","url":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1781352167","contentUrl":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1781352167","caption":"w3compadmin"},"sameAs":["http:\/\/w3computing.com\/articles"]}]}},"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"w3compadmin","author_link":"https:\/\/www.w3computing.com\/articles\/author\/w3compadmin\/"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/50","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/comments?post=50"}],"version-history":[{"count":5,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/50\/revisions"}],"predecessor-version":[{"id":84,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/50\/revisions\/84"}],"wp:attachment":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/media?parent=50"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/categories?post=50"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/tags?post=50"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}