r/learnprogramming • u/Happy-GoLucky365 • 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
72
Upvotes
View all comments
Show parent comment
16
u/Philluminati 5d ago
Just to be the contrarian (and for fun)… there are arguments that Python is demonstrably faster than C in terms of raw execution speed in certain scenarios.
You know Pythons REPL (console) is excellent. You type 2+2 and it immediately says 4. Way faster than you can compile a C program. Then you can reuse the answer in the next calculation. This is an important concept to keep in mind here.
C programs have their memory laid out at compile time and have a slow compilation phase. They also don’t have that lazy continuation that interpreted languages like Python have. Their REPLs rarely offer both their whole language and the benefits of the REPL itself.
If you want to transform a 10TB json file, Python and C would both be bottlenecked the time it takes to download the file to the machine in question … but since Python is interpreted… it doesn’t need to download the file… the code itself can be distributed without compilation over a 1000 node horizontally scalable Apache Spark cluster as a UDF. Each machine processes 100GB of data. C code basically can’t be, (unless someone write a c interpreter). This is why Python is more prevalent in big data and C isn’t.
Python can’t work with registers and memory layouts and beat C on a single machine, but C is so tightly coupled to a single machine architecture it makes a poor language to describe and implement cluster level computations.
If you don’t buy it, that’s fine. I write this out of boredom.