Can Cython Make Your Python Code 5× Faster? A Practical Speed‑up Guide

This article walks through turning a pure‑Python great‑circle distance function into a Cython‑accelerated version, compares timings, shows how to replace Python's math calls with C library functions, and demonstrates how pure C code can achieve up to a five‑fold performance boost.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Can Cython Make Your Python Code 5× Faster? A Practical Speed‑up Guide

I prefer Python for its elegant and practical syntax, but it is slower than many languages. Cython aims to combine Python's ease with C's speed by allowing Python syntax with C data types.

First, a pure Python implementation of the great_circle function (calculating distance between two points on Earth) is presented and timed over 500,000 calls, taking about 2.2 seconds.

import math

def great_circle(lon1, lat1, lon2, lat2):
    radius = 3956  # miles
    x = math.pi / 180.0
    a = (90.0 - lat1) * x
    b = (90.0 - lat2) * x
    theta = (lon2 - lon1) * x
    c = math.acos(math.cos(a) * math.cos(b) + math.sin(a) * math.sin(b) * math.cos(theta))
    return radius * c

Using Cython, the same function is rewritten with C type declarations. The Cython version still uses Python's math module, resulting in a modest speedup to about 1.8 seconds.

# cython: language_level=3
cdef extern from "math.h":
    float cosf(float)
    float sinf(float)
    float acosf(float)

def great_circle(float lon1, float lat1, float lon2, float lat2):
    cdef float radius = 3956.0
    cdef float pi = 3.14159265
    cdef float x = pi / 180.0
    cdef float a, b, theta, c
    a = (90.0 - lat1) * x
    b = (90.0 - lat2) * x
    theta = (lon2 - lon1) * x
    c = acosf(cosf(a) * cosf(b) + sinf(a) * sinf(b) * cosf(theta))
    return radius * c

Replacing the Python math calls with the C standard library functions (cosf, sinf, acosf) yields a significant improvement: about 0.4 seconds for the same workload, a five‑fold speed increase.

Further optimization is achieved by moving the inner loop into a pure C function and calling it from a thin Python wrapper, reducing the runtime to roughly 0.2 seconds.

// pure C version (great_circle.c)
float great_circle(float lon1, float lat1, float lon2, float lat2) {
    const float radius = 3956.0;
    const float pi = 3.14159265;
    const float x = pi / 180.0;
    float a = (90.0 - lat1) * x;
    float b = (90.0 - lat2) * x;
    float theta = (lon2 - lon1) * x;
    float c = acosf(cosf(a) * cosf(b) + sinf(a) * sinf(b) * cosf(theta));
    return radius * c;
}

The article concludes that Cython can provide dramatic speedups when the bottleneck is numeric computation or tight loops, but the effort should be justified by profiling results.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

performanceoptimizationPythonC extensionsCython
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.