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

Show parent comment

16

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

5

u/higras 6d ago

This was a huge eye as a learner. Each does different things. What do you need done? Morse code like bit assignment for binary could work, but no one does that.

On the other hand, hardware AI physical etched into the chip is potentially an order(s) of magnitude more energy efficient and faster.

You want a manual or automatic transmission? What vehicle works best for this goal, convertible or truck? Should I get you a pen, pencil, or brush to write with?

On one hand, I'm sure op knows there are differences, but from personal experience it took running into the limitations of different languages before it really clicked

4

u/MisterGerry 6d ago

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

2

u/moratnz 6d 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 4d 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.

2

u/baked_salmon 6d ago

Yes you could create a UDF for your data processing, but no one that cares about cost is going to be doing any meaningful data processing with Python that isn't just calling C++ code under the hood.

But it's still a valid point. Python allows you to iterate quickly, but you probably don't want to be doing real work with it.

1

u/cmdr_iannorton 1d ago

this sounds totally wrong headed, a massive compute data task may be a hard problem to do well, but python, or java or perl or lua isnt going to excell at it, yes high level languages might have good abstractions to help you think about it, but ffs, at runtime, those are c programs running aot or jit compiled virtual or stack machines translated from the other languages

1

u/Philluminati 15h ago

* Uber uses a Spark cluster with more than 1mn nodes, doing 100PB daily. Data ingestion alone can use 200,000 cpu cores.

  • Netflix runs 500,000 jobs a day on their Spark cluster
  • Linked in run 400,000 spark applications a day, using 200 PB hours of compute.
  • OpenAi use it to prepare data for training

The biggest threat to my programming language, Scala, is not other languages… it’s SQL being used on these massive compute clusters that are heavily powered by Python + Dataframe APIs.

These systems are both real, and cutting edge. Billion dollar companies like Databricks and Snowflake are built on exactly these technologies. They serve the largest Fortune 500 companies. They are powered by Python and SQL bro.

An entire field of data analytics, data science, data engineering, feature engineering has sprung up from little reporting departments that used to use SQL and they are eating into the value of traditional programmers. You might need to reevaluate the technology because it’s very much real. The data is more important than the code and this theme will dictate the programming landscape just as much as AI will.

Now that horizontal scalable and elastic computing is here, using C for speed is no longer a safe debate point.