r/learnprogramming • u/Happy-GoLucky365 • 5d ago
Why is C++ better than Python?
Hello! I am new to programming. Im in college and we use Python for my classes right now.
I hear people make a lot of jokes about how C++ is more powerful than Python, and should be used instead. But why?
Feel free to give me any tips as well, on things that helped you be a better programmer, or just any insight you may have! Thanks so much
63
Upvotes
View all comments
1
u/Atmosck 5d ago
C++ is not better (or worse) than python. They are very different languages with very different purposes. It's true that if you write code that does some number crunching in a loop, C++ will be way faster. That's becuase that kind of work isn't what python is for.
C++ is a compiled systems language. It gives you direct control of low-level system stuff like memory allocation, which is both powerful and dangerous. Compiling code can take a while, but that means the compiler can spend all the time it needs to optimize your code.
Python is an interpreted scripting language. With python there are more layers of abstraction between you and the hardware. You lose the low-level control of a systems language, but gain a lot flexibility, safety, and ease of use for it. Interpreted means the code is compiled in real-time while you use it*, which means a faster development workflow with no waiting for the compiler, but also slower code.
As a scripting language, python's job is to be an easy, flexible interface for orchestrating other things. Crucially, it dispatches to compiled extensions written in other languages for performance sensitive things. Much of the python standard library is C under the hood. Python's ecosystem is full of libraries that use extensions written in C, fortran or rust. That slow number crunching loop that's way faster in C++ - the right way to do that in python is to use a library like numpy and it's vectorized functions instead of a python loop, and you'll get similar speeds.
*(The python interpreter really uses a two-stage process with an intermediate representation called bytecode. Typically, the first stage is compiled to bytecode ahead of time - that's what a .whl file is. Then the python virtual machine does the second stage - translating bytecode into machine code - in real time.)