Engineering

15 Oct 2023

Updated 4 Nov 2026

1 minute read

Rewriting Python Modules in C++, for a Performance Boost

Translating a coordinate conversion package to C++ cut CPU time by about 91% with no change in memory use.

High-performance applications are often written in a lower-level language, such as C++ or Rust, rather than Python. The appeal of lower-level languages lies in the finer control they give over system resources, which can unlock a significant boost in performance.

With a foundational understanding of C++, from past work on microcontroller programming in C, I wanted to try moving a basic Python package to C++. As an Earth Science graduate working in software engineering, it seemed fitting to work on something geospatial. In previous projects I’ve used fmalina/blocl-bnglatlon1, which converts British National Grid (OSGB36) to latitude and longitude (WGS84). Whilst efficient, I suspected I could improve its performance by rewriting it in C++.

Aside from using constants, not much has been altered in the code. It is largely a straightforward translation to C++. The code is available on GitHub2.

1

Results

The following results were gathered over 100 runs.

ItemCPU time (× 10⁻⁴ seconds)Memory (MiB)
Python9.2745820.828125
C++0.8195820.812500
Difference8.455~0
  • Duration: processing in C++ took about 91% less CPU time. That is a large gain for code that runs repeatedly.
  • Memory: memory use was nearly identical between Python and C++, as expected.
2

Conclusion

This exercise was a useful lesson in the performance gains available from moving a small, CPU-bound module out of Python. The reduction in processing time shows how much language choice can matter in performance-critical code.

The result applies to this kind of tight numerical work. If a similar module is a measured bottleneck in your application, a direct translation to a lower-level language like C++ is worth considering.