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

67 Upvotes

184 comments sorted by

View all comments

Show parent comment

23

u/cmdr_iannorton 5d 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.

77

u/vegan_antitheist 5d ago

Both are Turing complete. There's nothing C can do that Python can't. The difference is performance. That's why you don't see many computer games written in Python. But someone ported DOOM to Python: https://github.com/hmofet/duum
You could port GTA6 to python and it would run. Just not at 30 fps.

3

u/no_more_cat_pics_pls 10h ago

Python can't be used for kernal or OS programming and it's lack of strong typing and no memory management make it much more prone to bugs and slower. You need very specific languages for certain things. C can't be used for websites like HTML but you can't just say they are turning complete and equal

1

u/vegan_antitheist 7h ago

Again: Both are Turing complete. There's nothing one can do the other can't. It doesn't matter how much some people want to deny this. I wouldn't even need examples to prove this, but those examples exist:

C can be and probably still is used for html. CGI programs built in C weren't even that uncommon in the 1990s. Amazon was just one example of a website made with C.

Amazon's first engineers coded in a computer language called C and decided to store the website in an off-the-shelf database called Berkeley DB

Source: The Everything Store: Jeff Bezos and the Age of Amazon (Brad Stone, 2012)

PythonOS is described as:

A bare-metal operating system where CPython 3.14 is the kernel — not a program running on an OS, but the OS itself.

Source: https://github.com/jordanhubbard/pythonos

•

u/no_more_cat_pics_pls 52m ago

Cool, sure a bike and a mustang can also both technically get you to the same place. Why not, both have wheels and theres nothing one can do that the other cant right?

-36

u/mysteryihs 5d ago

There's nothing C can do that Python can't. You could port GTA6 to python and it would run. Just not at 30 fps.

So there is something Python can't do, it can't run games more efficiently than C.

50

u/Talbertross 5d ago

Obviously it means there are no computations that C/C++ can do that Python can't. Pedantic dingus.

→ More replies (10)
→ More replies (1)

33

u/CodeToManagement 5d ago

You can write anything you want in assembly too. It doesn’t mean it’s better than any other language.

That’s why it’s about picking the right tool for the job. C++ is great for applications where you want more control over memory and much more optimised code execution. You don’t need that for some basic crud APIs for instance so it’s highly unlikely you’d benefit from the extra difficulty of using c++ when you could do it in half the time with Python.

15

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.

5

u/higras 5d 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 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.

→ More replies (1)

2

u/Philluminati 4d 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 5d 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 4h 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

13

u/baked_salmon 5d ago

Python is Turing complete, wdym you can’t write any program in Python?

4

u/Mwahahahahahaha 5d ago

They aren’t saying you can’t run any Turing complete program in python. They’re really saying python cannot fit certain resource requirements that c/c++ can. I.e., graphically demanding games, intense simulation software, modeling programs, etc.

1

u/100BottlesOfMilk 5d ago

It depends on what you mean by "in python". Python is best used when you're using libraries and the like which are implemented in a faster programming language

→ More replies (3)

1

u/Moscato359 2d ago

There are many things that can't be written in python simply because a feature of the application is it's performance 

2

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

While I almost 100% agree, I don't think there exists any program you can write in Python that trounces on its own memory and causes as segfault, or dereferences a null reference and crashes the program... At least not in a way that didn't depend on a bug in the Python runtime or a library. But in C/C++, it's perfectly possible to write a program that crashes. (And we've all accidentally done that! :)

1

u/cedarSeagull 5d ago

not all computers can run python

C'mon.... Every computer can run Python. No one counts your Windows Phone as a computer.

1

u/coconut_maan 5d ago

They optimize different things. Python optimizes development speed and maintainability with easy to access tools and easy to interface with services.

C++ optimizes low level real time performance. The tooling can be a bit brittle and time consuming.

If you want to make a shader or robot use c++. If you want to make a web app or aws pipeline use python

1

u/heisthedarchness 2d ago

Name a program you can write in C++ that you cannot write in Python.

1

u/cmdr_iannorton 4h ago

you cant write a python program if your environment can't run python, for example, you cant write a python bootloader or os kernel, you cant write microcontroller code or firmware in python (except for very special chips) you could write a python interpreter in python, but you'd have to run it using cpython or ipython etc.

i love python, but there is a reason for using it and a reason for not.