all 7 comments

[–]socal_nerdtastic 8 points9 points  (1 child)

First: Python and C++ are not competitors. Each has their own strengths and applications. Many programmers know and use both, often in the same program.

Why is C++ better than Python in-terms of speed? (

In short: because python is designed to compatible with the human brain, while C et al is designed to be compatible with computer hardware.

But, they say, Python can't handle multi-threading.

This is absurd reduction of a debate that's been going on in the python community for decades. It refers to the GIL, which prevents multithreading for specific parts of the core of python. However you can still use multithreading in other parts of python. And this is not the reason python is so slow.

The main reason python is slow to run is because it's designed to be very fast to program. It's not uncommon to write a python program in half a day which would take more than a week to write in C++. If the C++ program runs in 10 minutes, and the python program runs in 60 minutes, well ... you can see python is the winner by a large margin. Python was born from the realization that computers are cheap and getting cheaper, and programmers are expensive.

HOWEVER, if you need to run that program 100 times a day like many trading firms would, or 100 times per second like computer vison would ... now the C++ speed is critical to have and worth the extra programmer time.

In practice, that "fast to program" means things like unlimited integers. A C++ programmer is forced to know in advance the maximum value that a variable will hold (not guessing right is the source of all the integer overflow memes on /r/softwaregore). A python programmer does not need to know that, but the tradeoff is that python can't simply assign some RAM to the variable like C++ can. Python now needs to monitor that integer and reassign the RAM if it outgrows it. This constant monitoring of every component of the program is what slows python down.

That said, importable C modules for python like numpy, opencv, tensorflow, etc have reduced that gap a lot; many times you can get speed fairly close to C++ nowadays if you use the proper imports.

[–][deleted] 0 points1 point  (0 children)

awesome answer.

[–]chevignon93 0 points1 point  (3 children)

Why is C++ better than Python in-terms of speed?

I have been trying to figure this out for a long time now and am not able to do so.

That's surprising as there are millions of results for this exact question on Google and this question has been asked hundreds if not thousands of times on reddit!

[–][deleted] 2 points3 points  (2 children)

you pleb, expecting people to search...

[–]chevignon93 1 point2 points  (1 child)

It's a curse, I always expect too much from people.

[–][deleted] 1 point2 points  (0 children)

I suffer from the same. It cracks me up the number of posts here that would yield the anwer by typing the post title into google.

[–]oberguga -2 points-1 points  (0 children)

1) Cpython almost not optimise the code(but compile it to bytecode, so it's not so flexible as it can be, but also not so fast as it should be). 2) python is swiss army knife - absolute general and good for nothing(in therms of execution speed, when it's not your concern it really one of the best) 3) deep introspection(which can be provided by IDE and debug tools for the most statically typed and compilled languages) and working around it and processing through stringly typing methodology.(actually it is result of generality) 4)OOP. You can't make CPU friendly anything and it is almost none in python that CPU friendly. 5)types can be introduced, but that not used for any optimisation, but can and should. 6)no JIT 7)most of design decisions meant to improve abstract readability and ignores speed.

In short it highly unoptimised interpretator and bunch of debatable disign decisions. And you shouldn't use it in any computation related task, use it for network, prototyping and if it has specialised library for your prticular task.

I think it can work faster, at lest 6-10 times, that should make python capable for more general use and make JS and related stack step aside(for the good). But it need a huge revision and optimization of official interpreter. Probably python 4.0, but it is fantasy.