C++ vs. Python Empirical Runtime Analysis

Measuring the performance differences in C++ and Python for various Data Structures and Algorithms

Zach Wolpe
2 min readAug 6, 2023

Empirically testing the runtime characteristics of C++ and Python.

Python is an interpreted language, and the interpreter is running the same code over and over again. The C++ code is compiled to machine code, and thus runs much faster & is optimized to reduce superfluous calculations/repetitive tasks.

Build

Each implementation runs n operations of a combination of k data structure & algorithm implementations, all wrapped in a CPU profiler. The results are written to a .prof file that is then profiled externally (Using my psuedo-profiler tool for assistance).

The algorithms selected are common data structures & algorithms that are widely used in practice and have well-understood theoretical time and space complexity, serving as excellent benchmarks.

The complete code is available on GitHub.

Runtime Performance

As expected, the Python performance is exponentially slower than the equivalent C++ implementation in the number of operations.

Aggregate C++ vs Python Empirical Runtime by n Operations.
Slides (1)-to-(5) denote the runtime characteristics of the Python implementations, ranging from operations = {10,100,1000,10000,10000} respectively. The final slide (6) denotes the same profile, over all operation sets of the C++ runtime.

The complete code is available on GitHub.

--

--