
Python vs C++: What Each Is Good At, and Which to Learn First
Python is faster to write and C++ is faster to run, but that sentence hides the interesting part. Most fast Python is C or C++ underneath, Python 3.14 officially supports a build with no global interpreter lock, and memory-unsafe C++ is under pressure from federal guidance. Here is the comparison with the receipts.
Tech Talk News Editorial
9 min read
Key takeaways
- On the Computer Language Benchmarks Game, the fastest C++ n-body program (g++ 14.2.0) runs in 2.15 seconds and the fastest Python 3.13.0 version takes 372.41 seconds, but on pidigits the fastest times are 0.72 seconds versus 1.35 seconds, so the gap depends on what the Python code calls.
- CPython compiles your source to bytecode and an interpreter runs it, while C++ is compiled ahead of time to machine code; Python's own docs call bytecode an implementation detail that can change between versions.
- Python 3.13 introduced an optional free-threaded build with the GIL (global interpreter lock) disabled, and Python 3.14 made it officially supported under PEP 779 with a single-threaded penalty of roughly 5-10%, but the GIL-enabled build stays the default.
- C++ std::vector operator[] performs no bounds checking, and reading past the end is undefined behavior unless the implementation is hardened (a C++26 option), which is the kind of defect CISA and the NSA targeted in their joint memory-safe languages guide of June 24, 2025.
- NumPy's documentation attributes its speed to pre-compiled C code, and PyTorch's C++ documentation calls ATen the tensor library on which almost all other Python and C++ interfaces in PyTorch are built.
On this page10 sections
Python vs C++ is the comparison every new programmer runs into, and most of the answers online stop at one sentence: Python is easy, C++ is fast. That is true. It is also the least useful thing you can know about the two.
I went looking for the numbers behind that sentence and found that the interesting part is where the two languages meet.
The fast Python in machine learning is mostly C and C++ wearing a Python interface, and the language that was supposed to be the careful choice for performance is now the one federal guidance asks makers to publish a plan to move away from.
Python vs C++ in one answer: easy to write against fast to run
Python is faster to write and the better first language for most people. C++ is far faster to run on CPU-bound code and gives you control over memory and layout, but a mistake can mean undefined behavior instead of an error message. Real systems often use both: Python on top, C++ underneath.
That's the summary. The table below is the same answer broken into the decisions you'll actually face, and the sections after it say which rows I think matter most.
| Question | Python (CPython) | C++ |
|---|---|---|
| How does it run? | Source compiles to bytecode, and an interpreter runs the bytecode | A compiler turns source into machine code before you run it |
| Types | Checked at runtime; annotations are optional and not enforced by the runtime | Checked at compile time; every variable has a declared or deduced type |
| Memory | Reference counting, plus a cycle collector | Objects freed by destructors (RAII); manual allocation still possible |
| Out-of-range access | Raises an exception | Undefined behavior with operator[] |
| Threads on multiple cores | One thread runs Python at a time in the default build; optional free-threaded build since 3.13 | Threads run in parallel |
| Best at | Scripting, data work, machine learning, glue between systems | Game engines, databases, browsers, embedded and performance-critical libraries |
Python runs your code in an interpreter, and C++ turns it into machine code first
The default Python, called CPython, compiles your source into bytecode, then an interpreter executes that bytecode one instruction at a time. The standard library's dis module will show it to you, and its documentation is blunt that bytecode is “an implementation detail of the CPython interpreter” with no guarantee it won't change between versions.[1]
C++ skips the interpreter. A compiler translates the whole program to native machine code ahead of time, so the processor runs your logic directly. If you want the general version of this split, our post on compiled versus interpreted languages covers it without the C++ detail.
Typing follows from the same design. Python checks types while the program runs, and its documentation says the runtime “does not enforce function and variable type annotations”; checkers and editors read them instead.[2]
C++ settles types before the program exists, which is why it catches a whole class of mistakes at compile time and why its error messages can run to a page. If you want a feel for Python's side of that trade, our Python and Java syntax comparison puts it next to another statically typed language.
The same program in both languages shows the difference in a dozen lines
Here is one small job, summing the squares of the integers from 1 to one million, written the same way in each language. I ran both: they print the same answer, 333333833333500000.
def sum_squares(n: int) -> int:
total = 0
for i in range(1, n + 1):
total += i * i
return total
print(sum_squares(1_000_000))#include <cstdint>
#include <iostream>
std::int64_t sum_squares(std::int64_t n) {
std::int64_t total = 0;
for (std::int64_t i = 1; i <= n; ++i) {
total += i * i;
}
return total;
}
int main() {
std::cout << sum_squares(1'000'000) << '\n';
}The logic is identical. What differs is everything around it. The C++ version states a type for each number and needs an include, a main and a build step. The Python version runs as soon as you save it.
There's a trap hiding in the C++ version, and it's the one difference I'd want a beginner to remember. Python integers grow as large as memory allows.
C++ integers have a fixed width, so a larger input to this function would overflow a 64-bit signed number, and signed overflow in C++ is undefined behavior, not a wrapped result. The Python program would print a bigger number, with no warning needed.
A benchmark gap is real, but it tells you about one program
You'll see claims that C++ is 10 times, 50 times or 100 times faster than Python. Treat any single multiplier as suspect, including the ones below, because the answer depends on the program.
The most reproducible public source I know of is the Computer Language Benchmarks Game, which publishes the source code, versions and timings for each program.
In its Python 3 versus C++ g++ comparison, using Python 3.13.0 and g++ 14.2.0, the fastest n-body program in C++ runs in 2.15 seconds. The fastest in Python takes 372.41 seconds, about 173 times longer (372.41 divided by 2.15).[3]
On pidigits, the same page shows 0.72 seconds for the fastest C++ and 1.35 seconds for the fastest Python, under two times apart.[3] The page labels that benchmark “Contentious. Different libraries.” Big-number arithmetic is the work, and Python programs can hand it to compiled library code.
Read those numbers the way the project asks you to. The measurements come from one quad-core 3.0GHz Intel i5-3330 running Ubuntu 24.04, and each time is the lowest elapsed time from 12 BenchExec measurements.[4] The authors also reject using their tiny programs to “define the relative performance of programming languages.”[5]
So here is my read. A tight numeric loop that runs in the Python interpreter pays for every bytecode instruction, and no amount of clean code changes that. A program that spends its time inside compiled libraries barely notices the interpreter. Knowing which kind you have is worth more than any multiplier.
Most fast Python is C or C++ with a Python front door
This is the point most comparisons skip. When a Python data scientist says their code is fast, it's usually because the loop isn't running in Python at all.
NumPy's documentation says its vectorized operations happen “behind the scenes” in “optimized, pre-compiled C code,” saving “all the overhead involved in interpreting the Python code and manipulating Python objects.”[6] You write c = a * b, and the loop runs in C.
PyTorch works the same way at larger scale. Its C++ documentation describes ATen as “fundamentally a tensor library, on top of which almost all other Python and C++ interfaces in PyTorch are built.”[7]
GitHub's language breakdown of the PyTorch repository (read on November 29, 2026) lists about 118 million bytes of Python, 47 million of C++ and 4.5 million of CUDA.[8]
Notice that Python is the biggest share there, so “it's all C++ underneath” overstates it. A lot of that Python is tests and framework code. The accurate version is that the numeric kernels live in compiled code and Python orchestrates them.
“Python is how most people write the program. C and C++ are what the program mostly runs.”
That changes the choice. If you want to work in machine learning, you aren't picking between Python and C++. You're picking Python and deciding later whether you ever need to write the layer beneath it.
Python counts references and C++ ties cleanup to scope
Memory is the second big difference. CPython tracks how many references point at each object; “once the last strong reference is released (i.e. the object's reference count reaches 0),” the object is deallocated.[9]
Because counts can't see cycles, a garbage collector “supplements the reference counting already used in Python.”[10] You rarely think about any of this, which is the point.
Modern C++ ties cleanup to scope. The pattern is RAII, resource acquisition is initialization, which binds a resource's life cycle to an object's lifetime and releases it when that object goes out of scope, including on an exception.[11] It's deterministic and costs nothing at runtime. It also only protects the code that uses it.
The catch is everything RAII doesn't cover. Take the humble std::vector. Its operator[] performs “no bounds checking,” and reading past the end is undefined behavior, unless the implementation is hardened under C++26.[12]
The same mistake in Python raises IndexError. I've lost an afternoon to a C++ bug that behaved differently under a debugger, and that is exactly what undefined behavior feels like.
Plain English
Python 3.14 officially supports a version with no GIL, and it's still optional
The other long-standing Python limit is the global interpreter lock (GIL). PEP 703 states the problem plainly: the GIL “prevents multiple threads from executing Python code at the same time,” and is “an obstacle to using multi-core CPUs from Python efficiently.”[13]
The fix is a separate build. The Python documentation says that starting with 3.13, CPython supports a free-threaded build where the GIL is disabled, and the official macOS and Windows installers optionally include it.[14] PEP 703 itself said the GIL “will remain the default for CPython builds and python.org downloads.”[13]
Python 3.14 moved the status up a step. PEP 779 defines three phases: experimental, “officially supported but still optional,” and finally the default. Phase II begins in 3.14.[15] The 3.14 release notes put the single-threaded penalty at “roughly 5-10%,” depending on platform and compiler.[16]
The docs also warn that some third-party packages with extension modules “may not be ready for use in a free-threaded build, and will re-enable the GIL.”[14] That matters because the Python ecosystem leans on those extensions.
My read: free-threading is a real shift, and you can try it today, but don't rewrite a production service around it until your dependencies are on the list. Check the release notes for the Python version you actually run.
For CPU-bound parallel work, C++ threads still run on every core with no such caveats. Python is closing that gap, and it hasn't closed it.
Memory safety is the pressure C++ is under now
On June 24, 2025, CISA and the NSA published a joint guide on memory safe languages, recommending steps such as language-level protections, library support and developer training.[17]
Our breakdown of the federal memory-safety push covers what the guidance says, which languages it calls memory safe (Python is on the list) and why C and C++ are the ones it steers away from.
CISA's Product Security Bad Practices guidance, revised January 17, 2025, expects makers of existing products in memory-unsafe languages to publish a memory safety roadmap. It calls new critical-infrastructure product lines in C or C++ dangerous when a memory-safe language is readily available.[18]
This isn't a verdict that C++ is finished. It means the safer-than-C++ conversation has real candidates. If that's the part you care about, our comparison of Rust and Go for backend work is where I'd go next.
Side note
Learn Python first, and learn C++ when something forces you to
If you're deciding between them today, here is the rule I'd give a friend. Pick Python if you want to build things, work with data, or get into machine learning.
Pick C++ if you already know you want game engines, embedded firmware, high-frequency trading or the guts of a database, and you can afford a steep first year.
Everyone else should start with Python and treat C++ as the second language you reach for when a profiler points at a hot loop. By then you'll understand what C++ is buying you. I'd take that over a beginner fighting a linker error in week two.
Takeaway
Python is the better language to start in and C++ is the better language to run in. Most of what you'll do with Python in 2026 already runs on C or C++, so the real choice is which layer you want to own.
Sources and further reading
- 1.PrimaryPython documentation, "dis: Disassembler for Python bytecode". CPython bytecode is an implementation detail with no guarantee of stability between versions.
- 2.PrimaryPython documentation, "typing: Support for type hints". The runtime does not enforce function and variable type annotations; third-party tools use them.
- 3.DataThe Computer Language Benchmarks Game, "Python 3 versus C++ g++". Elapsed seconds for n-body (C++ 2.15, Python 372.41) and pidigits (C++ 0.72, Python 1.35); Python 3.13.0 and g++ 14.2.0.
- 4.DataThe Computer Language Benchmarks Game, "How programs are measured". Quad-core 3.0GHz Intel i5-3330, Ubuntu 24.04; lowest elapsed time from 12 BenchExec measurements.
- 5.PrimaryThe Computer Language Benchmarks Game, "Why measure toy benchmark programs?". The authors reject using a few tiny programs to define the relative performance of languages.
- 6.PrimaryNumPy documentation, "What is NumPy?". Vectorized operations run in optimized, pre-compiled C code, avoiding Python interpretation overhead.
- 7.PrimaryPyTorch, "PyTorch C++ API". ATen is a tensor library on which almost all other Python and C++ interfaces in PyTorch are built.
- 8.DataGitHub API, language breakdown for the pytorch/pytorch repository. Bytes of source by language, read November 29, 2026: Python 118,379,966, C++ 47,469,101, CUDA 4,545,597.
- 9.PrimaryPython documentation, "Reference Counting". An object is deallocated when its last strong reference is released and the count reaches 0.
- 10.PrimaryPython documentation, "gc: Garbage Collector interface". The collector supplements the reference counting already used in Python.
- 11.Primarycppreference, "Resource acquisition is initialization". RAII binds a resource life cycle to an object lifetime, releasing it on scope exit or exception.
- 12.Primarycppreference, "std::vector::operator[]". No bounds checking unless the implementation is hardened (C++26); out-of-range access is undefined behavior otherwise.
- 13.PrimaryPython Enhancement Proposals, "PEP 703: Making the Global Interpreter Lock Optional in CPython". The GIL prevents multiple threads from executing Python code at once; the GIL stays the default build.
- 14.PrimaryPython documentation, "Python support for free threading". Free-threaded builds since 3.13, optional installers, and extension modules that re-enable the GIL.
- 15.PrimaryPython Enhancement Proposals, "PEP 779: Criteria for supported status for free-threaded Python". Phase II, officially supported but still optional, begins in Python 3.14; Phase III makes it the default.
- 16.PrimaryPython documentation, "What's new in Python 3.14". Free-threaded Python is officially supported; the single-threaded penalty is roughly 5-10%.
- 17.PrimaryCISA, "Memory Safe Languages: Reducing Vulnerabilities in Modern Software Development". Joint CISA and NSA guide published June 24, 2025.
- 18.PrimaryCISA, "Product Security Bad Practices" (version 2.0, revised January 17, 2025). A published memory safety roadmap is expected for existing products in memory-unsafe languages; new critical-infrastructure product lines in C or C++ are flagged as dangerous where memory-safe alternatives are readily available.
Frequently asked questions
5 questions
Is C++ faster than Python?
Yes, on CPU-bound code, often by a wide margin. On the Computer Language Benchmarks Game, the fastest C++ n-body program takes 2.15 seconds against 372.41 seconds for the fastest Python 3.13.0 one. The gap shrinks when Python hands the work to compiled libraries: on pidigits the fastest times are 0.72 seconds for C++ and 1.35 seconds for Python.
Should I learn Python or C++ first?
Learn Python first unless you already know you're headed for game engines, embedded firmware or other work that needs control of memory. Python lets you write and run a working program in minutes and shows you the ideas of programming without a compiler in the way. You can add C++ later, and by then you'll understand what it's giving you.
Is Python or C++ better for machine learning?
Python is the language you write machine learning code in, and C++ and CUDA are largely what runs underneath. NumPy's documentation says its element-by-element operations run in pre-compiled C code, and PyTorch's documentation describes ATen, a tensor library, as the base for almost all of its Python and C++ interfaces. Most practitioners never write the C++ layer.
Can Python run without the GIL?
Yes, in the free-threaded build. Python 3.13 added it as an optional build, and Python 3.14 made it officially supported under PEP 779. The GIL-enabled build is still the default, the free-threaded build carries a single-threaded penalty of roughly 5-10%, and some extension modules re-enable the GIL when they load.
Is C++ going away because of memory safety?
No, but it is under pressure. CISA and the NSA published a joint memory-safe languages guide on June 24, 2025, and the CISA Product Security Bad Practices guidance (revised January 17, 2025) expects makers of existing products written in memory-unsafe languages to publish a memory safety roadmap. It also calls new critical-infrastructure product lines in C or C++ dangerous when a memory-safe language is readily available. Existing C++ systems are not being switched off.

