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

71 Upvotes

186 comments sorted by

View all comments

Show parent comment

4

u/MisterGerry 5d ago

What the hell are you talking about?
No. Just... NO.

2

u/moratnz 5d ago

If the relevant metric is 'time to first successful run of the code' not 'time for a single execution of the code, once you've finished prepping the code', python is a really fast language.

1

u/GarThor_TMK 3d ago

That seems to be philluminatis only metric...

Write once, run once, keep the results, toss everything you did to get the results.

For this, Python excels...

For anything else, it's a wasteful and unoptimized way to run your code.

0

u/moratnz 3d ago

Though much of the time the waste and lack of optimisation is irrelevant.

If C delivers the result in a microsecond and python delivers it in a millisecond then C is a thousand times faster, and for low-scale applications that thousandfold speed difference is completely irrelevant, meaning python's ease of maintenance makes it strictly superior.

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.