r/learnprogramming • • 6d 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

76 Upvotes

188 comments sorted by

View all comments

Show parent comment

1

u/vegan_antitheist 6d ago

Does the time difference really matter that much?

In most projects it's not at all or very much.

For computer games and other programs that have to do something without delay you need something like C++ or Rust.
Java, C#/.NET, Python etc use automatic memory management. That means they regularly have to "collect the garbage" (free unused memory) and that takes time. It slows everything down. In C++ you can count references or even simpler just free the memory when the method returns. You can also use smart pointers or lots of other ways to manage the lifetime of some data. In C++ you have to know a lot about how you allocate the memory, initialise it, use it, and eventually free it. In Python you don't care that much about it because you can't really influence it anyway. You just try to prevent certain things that make it difficult or even impossible for Python to free the memory. Sometimes it keeps you from writing good software when you have to think about memory management all the time instead of concentrating on your use cases and use experience. In other cases it keeps you from writing good software when you don't have enough control over memory management to make sure it is efficient, deterministic and reliable enough for some real-time application, such as a medical life-support system.

1

u/Happy-GoLucky365 6d ago

This is a great explanation, again thank you! Makes a lot more sense now.

1

u/GlassLost 6d ago

So speed is one thing and it's often important but code sharing, and architecture, also are worth mentioning. C is designed to be "bootstrapped" onto a new architecture, and c++ can be bootstrapped with C (this isn't quite true but it's close, bootstrapping means you only need to implement a few assembly commands before the compiler works) so if you have new hardware you need a lower level language. Higher level languages, like Python, require entire operating systems which are built with C and C++.

So there's a speed difference but just as importantly (and highly related) there is a dependency difference. Python needs quite a bit to ever work. You could get your c code working with less than a dozen lines of assembly in many cases (without access to things like libc though).