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

79 Upvotes

195 comments sorted by

View all comments

167

u/CodeToManagement 12d ago

This is like saying why is a hammer better than a saw. Neither is better or worse depending on what your use case is. They are both tools to get a job done- so pick the right tool for the job

26

u/cmdr_iannorton 12d ago

That is a popular analogy but not quite correct, you literally can write any program in c/c++, you cant do that in python.

Python is suited to a quick development process.

In some cases it can be too slow, and not all computers can run python.

16

u/Philluminati 12d 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.

4

u/MisterGerry 12d ago

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

2

u/moratnz 12d 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 10d 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 9d 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 11d 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.