When to use C and when to use Python

Published: July 5, 2017, updated: January 20, 2025

I’ve now solved the first 15 Project Euler challenges in C. But then, I’ve hit a road block. Let me explain.

Let’s say you want to find the sum of digits for the number 3^300. Would you use C or Python?

Python

In Python, it’s easy:

sum(map(int, str(3**300)))

C

To achieve the same thing in C you would need:

  1. A big decimal library
  2. A big decimal to string conversion function
  3. Use manual memory allocation
  4. Worry about printing the result correctly

All this without ever having the chance to express on a high level:

Calculate 3^300, find the digits, convert them to integers, sum them.

Instead, C would require you to say:

Initialize my big decimal library, start the calculation. Check whether the calculation did not run out of memory. Then, carefully execute a loop that uses the big decimal to string conversion routine to retrieve its string representation. Then, convert this string representation back to integers, and then finally sum the integers.

While you can always create a nice high level abstraction inside of C, the drawback is flexibility. While you carefully crafted all this code within an hour or so, using Python lets you solve 20 similar problems in the same time, while providing superior memory safety, readability, and maintainability.

This comes with some drawbacks:

  1. The Python code is much slower. It requires tons of Python object meta-data and requires garbage collection.
  2. The C code can be optimized much more easily by using the correct compiler flags.
  3. The C code can easily interface with assembly instructions that might be offered on your CPU to offer further speedups.

The Compromise

A great solution that a lot of Python projects find is to have the high level API run in Python, while the low-level hot loops are written in C. One of the most famous projects that do it this way would be NumPy. It contains C code for efficient array packing, but still exposes a powerful Python interface to achieve a lot in a small amount of LOC.

Tags

I would be thrilled to hear from you! Please share your thoughts and ideas with me via email.

Back to Index