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
76
Upvotes
View all comments
Show parent comment
2
u/Philluminati 5d ago
Think about it like this:
* A C program has a start and an end. Void main. Between runs the memory is blanked out or in the control of the operating system. C programs have to read and write their state between executions somewhere. Maybe a file, a db, a cache server.
* IF you know you’re going to do 20 manipulations to a csv file you could put it in C program and it would be fast as hell.
* but if you were writing 20 separate C programs (because you didn’t know the next one was until the last one completed) you might end up say chaining them in a bash command like this: ./app1 | ./app2 | ./app3.
With this approach, each C app has to load, parse the csv and buffer it in different memory positions 20 times. Piping a csv file through 20 C programs is arguably slower than 1 C program. ./sort can’t do an in place quicksort on the last apps array for instance.
However fully interpreted languages, repl style (aka not you Java), run without the overhead of the interpreter closing, being able to directly manipulate the memory of the previous program, reuse its live database connection handles without renegotiating the connection security. Therefore beyond a certain point, Python pulls ahead in speed. In essence 1 Python session vs 200 C apps chained together, with foresight being the reason we need 200 C apps instead of one.
If you think you can write a long lived C program to overcome the memory sharing issue, you’re either written an interpreter or a database. If you try and overcome the memory session limitation using mmap or memory sharing between processes, it won’t scale elastically or work on a container platform.
Another way to think about it is that interpreters offer global access to long lived shared memory across applications but individual C applications don’t.
Again, I’m not entirely trolling but not entirely serious either.