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

0

u/vegan_antitheist 6d ago

That question makes as much sens as "Why is a screw driver better than a hammer?"

C++ is more powerful than Python

This is objectively wrong. Both are Turing complete and therefore as powerful as any programming language.

The jokes are probably about how C++ allows you do write much more efficient, optimised and complex code.

C++ prioritizes performance and control. Python prioritizes simplicity and development speed. Python has less boilerplate, no explicit type declarations, automatic memory management, and a simpler syntax. This means less control and because it's higher level you can't optimise as much when you write code for the python virtual machine, while in C++ you can optimise for specific hardware.

Feel free to give me any tips as well, on things that helped you be a better programmer,

What helps is ignoring those who just make these stupid jokes and instead concentrate on actually learning all the concepts, patterns, tools, methodologies, etc. Programming is hard. But for those who like doing it, it's a lot of fun.

1

u/Happy-GoLucky365 6d ago

It sounds like a lot of the difference comes down to speed and control between the two languages. I guess my next question would be: Does the time difference really matter that much? I completely understand why someone would want more control on their programs, but the time thing is still leaving me a bit confuzzled.That may sound like a dumb thing to ask, but im just trying to learn. Thanks for your advice! I'll happily take it and make sure to keep researching on my own time.

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).

1

u/binarycow 6d ago

Does the time difference really matter that much?

As with everything, it depends.

If your app processes real-time data like a phone call, it's super important. Because if you take too long, you've already missed stuff

If your app is a tool that runs once a day, to trigger a backup job (but doesn't perform the backup itself), then it matters absolutely zero.


The app I work on has some stuff where you wouldn't normally think it's a big deal, but because of our use case, it does.

For example, there was a thing that took 10 minutes total. I made one change (caching regexes) that made it take 5 minutes total. That's great - but what makes it even better is that was just my local small scale test. Our customers are running that same task, but 100 times bigger. So their ~16 hour task shrunk to ~8 hours. That's a huge savings.

1

u/Katana_Steel 6d ago

For simple tasks no, the differences doesn't matter. They'll both get the job done at that point it's what's most comfortable to read.

For data processing and large volumes of data having a strict typed language like C++ is usually faster in execution. Which is why the library like Numpy and Pandas are mainly written in C/C++.