Introduction
In the world of programming languages, Python has long been an outlier. It is dynamically typed, interpreted, and notoriously slower than compiled languages like C++ or Rust. Benchmarks consistently show Python executing loops and numerical computations 10 to 100 times slower than its lower-level counterparts. Yet, paradoxically, Python has become the undisputed monarch of machine learning (ML) and neural network development. How did a language that struggles with raw speed come to dominate the most compute-intensive field in software engineering? A recent in-depth analysis on Habr (published July 2026) explores this phenomenon, revealing that Python's success is not despite its slowness, but because of a unique ecosystem of abstractions and tooling that shifts the performance burden to optimized backends.
The Performance Paradox: Python vs. C++ in Neural Network Training
To understand the paradox, we must first quantify the gap. In classic CPU-bound tasks, Python code is often 20–50× slower than equivalent C++. For example, a simple matrix multiplication of size 1000×1000 can take ~0.5 seconds in pure Python, while the same operation in C++ with optimized loops takes ~0.01 seconds. However, neural network training does not rely on pure Python loops. Instead, the heavy lifting is delegated to highly optimized libraries written in C, C++, and CUDA.
The Secret: PyTorch and TensorFlow as C++ Wrappers
The core insight, as detailed in the Habr article, is that Python acts as a glue language for neural networks. Frameworks like PyTorch and TensorFlow provide Python APIs, but the actual tensor operations, gradient computations, and GPU kernels are executed in compiled code. When you run torch.matmul(A, B), Python merely dispatches a call to a precompiled C++ library (ATen) or a CUDA kernel. The Python interpreter spends microseconds marshaling data, while the C++ backend spends milliseconds—or seconds—computing. The performance bottleneck is not Python; it is the network bandwidth and GPU computation time.
Example from the article: The Habr authors show that for a typical ResNet-50 training loop, Python's overhead accounts for less than 1% of total time. The remaining 99% is spent in cuDNN routines and data loading (which can be parallelized via PyTorch's DataLoader). Thus, Python's slowness is amortized over massive parallel operations.
NumPy: The Foundation of Speed
Before deep learning, NumPy revolutionized scientific computing in Python. It introduced the ndarray object, which stores data in contiguous C arrays and performs vectorized operations without Python loops. A vectorized addition a + b in NumPy runs at C speed, avoiding the interpreter overhead. The Habr article notes that NumPy's design—with its strided memory layout and broadcasting rules—directly inspired PyTorch's Tensor class. Without NumPy's precedent, the deep learning ecosystem might have never emerged.
JIT Compilation and Tracing: Closing the Gap Further
Modern Python ML tools have also adopted just-in-time (JIT) compilation to reduce overhead. For instance, PyTorch's torch.jit.trace and torch.compile (available since PyTorch 2.0) convert Python functions into optimized TorchScript or machine code. The Habr article reports that in 2025–2026, torch.compile with the inductor backend can achieve speedups of 1.5–2× over eager mode for many models, making Python code competitive with hand-written C++ for model inference.
| Framework | Backend | Typical Speed (vs. Eager Python) |
|---|---|---|
| PyTorch (eager) | C++/CUDA | 1× (baseline) |
| PyTorch (compile) | Inductor/LLVM | 1.5–2× |
| TensorFlow (graph) | XLA/LLVM | 1.3–1.8× |
| JAX (JIT) | XLA | 1.5–3× |
The Ecosystem Effect: More Libraries, More Developers
Python's dominance is also a network effect. As of 2026, the Python Package Index (PyPI) hosts over 500,000 packages, with more than 10,000 specifically for ML and data science. The Habr article emphasizes that Python's ease of learning and readability attracts researchers, who prioritize experimentation speed over execution speed. A 2025 Stack Overflow survey found that 48% of professional developers use Python, and among ML practitioners, the share exceeds 80%. This critical mass fuels a virtuous cycle: more libraries → more users → more libraries.
Real-World Case: Training GPT-Scale Models
To illustrate Python's viability, the Habr article presents a case study of training a 7-billion-parameter language model (similar to LLaMA) on a cluster of 512 A100 GPUs. The training code was written in PyTorch with the DeepSpeed library for distributed training. Despite the complexity of pipeline parallelism and gradient checkpointing, Python code comprised only about 15% of the total lines. The rest was configuration files (YAML) and shell scripts. The training took 30 days, but Python's overhead accounted for less than 0.5% of total wall-clock time. The bottleneck was inter-GPU communication and memory bandwidth—problems no language can solve with syntax improvements.
The Role of High-Level Abstractions: Hugging Face and Keras
Another factor is the abstraction layer. Libraries like Hugging Face Transformers and Keras provide high-level APIs that let developers define neural networks with minimal code. For example, initializing a BERT model in Python takes 3 lines: from transformers import AutoModel; model = AutoModel.from_pretrained('bert-base-uncased'). Under the hood, this triggers loading of ~110 million parameters from disk, allocation of GPU memory, and compilation of the compute graph—all in C++. The Python part merely handles serialization and logging. The Habr article notes that Keras, now natively integrated into TensorFlow, allows researchers to prototype new architectures in hours rather than weeks.
Limitations and the Rise of Mojo
Despite Python's success, it is not without critics. The Habr article discusses the emergence of Mojo (a superset of Python with optional static typing and compiler optimizations) and the continued use of C++/CUDA for production inference engines (e.g., TensorRT, vLLM). Mojo, introduced by Chris Lattner in 2023, promises Python-like syntax with C-like performance. However, as of 2026, Mojo's ecosystem is still immature, lacking support for many PyTorch ops and Hugging Face integrations. The article concludes that Python will likely remain the dominant language for research and prototyping, while production systems may use a hybrid approach.
Conclusion
Python's reign in neural networks is a testament to the power of abstraction and ecosystem. The language itself is slow, but it provides a thin, elegant layer over a mountain of optimized C++ and CUDA code. The Habr article's analysis confirms that Python's overhead is negligible in practice, thanks to vectorization, JIT compilation, and GPU acceleration. As the field evolves, Python's lead is challenged by newer languages like Mojo, but for now, Python remains the king—not because it is fast, but because it makes everything else fast. For anyone interested in the technical details, the full analysis is available at the original source: Source.
Key takeaway: When choosing a language for ML, prioritize ecosystem and productivity over raw speed. Python's design, combined with modern frameworks, turns a slow interpreter into a powerful orchestrator of high-performance computing.
Comments