I was reading this post: http://www.quora.com/What-are-the-advantages-of-Python-over-C++

And I am wondering the converse of this question: what can C++ do that Python cannot?

For example, in the realm of performance graphics and GUIs: I understand that C++ would be a better language for high-end game development that requires fancy graphics and great performance, but as far as making GUIs I don't understand the tangible difference between using C++ and QT, or choosing PyQt.

If someone could please make a point-by-point quick rundown like this (e.g., GUIs, high performance graphics, building command line tools, concurrency, etc), it would be very helpful for me to decide whether to choose C++ or Python for my next programming project.

  • 3
    Run on the bare metal. – whatsisname Dec 3 '14 at 6:13
  • 3
    recommended reading: Gorilla vs Shark -- "if you... don’t want your question to get instantly closed... — try to keep Gorilla vs. Shark in mind." – gnat Dec 3 '14 at 6:37
  • 2
    @gnat This question has none of the attributes that make gorilla vs shark a bad question. It asks for specific and limited facts about the differences between two things of similar types. It seems to me to be a perfectly reasonable question. – Jules Dec 3 '14 at 9:25
  • 1
    @gnat But that isn't what this question is about. It may be the motivation for the question, but the question itself isn't "should I use Python or C++ for my next project?" Instead, it asks a reasonable technical question with well-defined answers that are not matters of opinion. I still see nothing wrong with this question. – Jules Dec 3 '14 at 9:32
  • 1
    @Jules that's what I read in the question: "it would be very helpful for me to decide whether to choose C++ or Python for my next programming project" – gnat Dec 3 '14 at 9:56

Python is a dynamically-typed language. This has two important consequences:

  • The compiler is unable to reject certain kinds of logical error at compile time which would be caught by a C++ compiler
  • Because the types of some (even most) variables cannot be determined at compile time, operations on those variables must be implemented by dynamically dispatching a method call (a fairly slow operation) whereas C++ compilers are often able to generate inline code (which has no overhead). C++ is therefore an inherently faster language.

The former of these is actually surprisingly useful. See for example the Boost Units library, which allows scientific and engineering code to be written in a way that allows the compiler to statically check that the calculations have logically consistent unit usage at compile time, with no runtime overhead. This would be impossible in Python.

Python also has very poor support for multithreading. Its language design requires certain operations (for instance modifications to lists) to complete atomically, which has lead to the most common implementation using a global lock to prevent two threads executing Python code at the same time (although if one thread calls native code the other will be allowed to procede), because the overhead of locking and unlocking individual objects made single threaded performance terrible.

C++, on the other hand, leaves the job of ensuring code is thread-safe up to the developer, which allows for efficient multithreading.

C++ also gives developers much more control over memory management, allowing for example generic containers that allocate user-specified object types inside their structures (leading to lower overhead and better locality of reference than is possible with Python), allocating objects in specified memory regions (e.g. so that multiple objects can be deleted in a single operation, or to allocate them in a memory-mapped file) or using developer-supplied custom allocators.

C++ is also loosely typed, which means you can use an area of memory for one type of data, and then change to treating it as containing another type. While operations of this type are dangerous (and the language specification states that programs that do this have "undefined behavior"), if you know what you're doing you can use this to implement dynamic loading of code modules, runtime native code generation, direct manipulation of file structures without individually extracting fields as you would have to in Python, and a wide variety of other uses. This is why operating systems can be implemented in C++ and not in Python.

|improve this answer|||||
  • 1
    the "static typing is inherently faster than dynamic typing" myth has been proved wrong several times by modern JITs – Javier Dec 3 '14 at 13:26
  • 2
    I keep hearing this, but benchmarks I've seen suggest that the best (v8) is about 10-50% slower than c++, and the best implementation of python is closer to 100% slower. I'd like to see anything that proves me wrong, but my searches so far haven't found it. – Jules Dec 3 '14 at 15:17
  • 1
    @Javier You say that as if it's not possible to write a modern JIT for a statically-typed language. No matter how you cut it, static typing gives you more information to make optimizations with, all else being equal. And to get comparable (and to keep your sanity high and bug count low) you have to use the dynamic language like a statically-typed language. Reflection black magic is a great way to kill performance. – Doval Dec 3 '14 at 17:22
  • 1
    @Javier Back in 2012 someone translated four randomly-selected Python libraries to Haskell. He was able to translate all of them line by line (i.e. it wasn't a full program rewrite, he preserved the original code's structure). To me that's a strong indicator that in practice people restrict themselves to a coding style that's implicitly statically-typed. In the process, he found a couple of type-related bugs that snuck past the libraries' unit test suites. – Doval Dec 3 '14 at 18:56
  • 1
    @javier ok, so which dynamic jit is faster than c++? V8 isn't, psyco isn't, and parrot wasn't. I've not seen anyone claiming spectacular speeds for ruby, and lua is about 20-30 times slower than c++ for most tasks. So where are these high-performance jit systems? – Jules Dec 4 '14 at 12:04

Programs which require real-time number crunching (such as digital audio workstations or video players) have what I call a "computational threshold." What that means is that the choice of programming language can matter when there is not enough hardware horsepower to satisfy the necessary computational load, if the language itself is consuming a substantial part of the CPU's clock cycles. In other words, if your language is not efficient enough, the application may not work at all.

Consequently, the choice of language depends on whether such a threshold exists. Naturally, you will have a better chance at writing a functional video player if you do it in a language that is highly efficient, like C++. The tradeoff is that you will generally write more code (and it will take longer to write), but that is generally true of applications like video players anyway.

For business applications with form-based GUI's, python is often chosen over C++ because, generally speaking, python allows you to code at a higher level of abstraction and with fewer intellectual hurdles than C++ does. Faster prototyping is thus made possible.

Of course, these are broad generalizations, and your mileage may vary due to factors like the speed of the processor, the number of available cores, the size of the installed RAM, whether you're cross-compiling the python to C, etc, making any definitive decision of which language to use for which application impossible to predict. As will all performance-related things, measure, don't guess.

|improve this answer|||||
  • 5
    Put another way, C++ offers tools to meet a requirement of completing a computation within a resource-limited (time-limited, memory-limited, etc.) window. For example, many games have the requirement that all work done for a given frame must be completed in less than 16 milliseconds to achieve a consistent 60 frames per second. The more complex the work, the harder it is to stay within that window. – greyfade Dec 3 '14 at 6:17
  • For such programs, I prefer the simplicity & speed C to C++ – Mawg says reinstate Monica Dec 3 '14 at 15:29

It is notably an implementation issue, not a language one (however the typing is different in the languages).

Pedantically, both Python & C++ are Turing-complete languages with a lot of bindings to external libraries, so every program you could write in Python could be rewritten in C++ and vice versa.

On Linux, /usr/bin/python (a.k.a. cpython) is a bytecode interpreter but /usr/bin/g++ is a native compiler

So very often a C++ program would be faster than the equivalent python program.

notice that there are experimental python compilers (e.g. pypy) and experimental C++ interpreters.

So C++ is good for programs which need to be fast, but may accept more development time, and Python is good for programs which need to be written quickly.

You probably won't write a Unix shell or an OS kernel (or even an optimizing C++ compiler) in Python (but in C++ or C), and you probably won't write a sysadmin script or utility in C++ (but in Python).

Also, a C++ program can usually be distributed as a self-contained executable (either statically linked, or using many C++ shared libraries).

At last, C++ is statically typed, and Python is dynamically typed. This means that you would need to do whole program type analysis to compile Python to efficient code. However, some Javascript JIT implementations (e.g. v8) seems to show that dynamically typed languages might be efficiently compiled with a lot of efforts (on implementation development). But nobody paid for such an effort on a mythical Python whole-program compiler implementation.

BTW, some Common Lisp implementations (e.g. SBCL) brings you a dynamic typing and an expressivity close to Python with a performance close to C++.

|improve this answer|||||
  • 1
    This is also a language issue however: C++ has pointers. – Florian Margaine Dec 3 '14 at 8:08
  • Indeed C++ & Python are different languages, but performance is related more to implementation (a software) than to language specification (a technical document) – Basile Starynkevitch Dec 3 '14 at 8:14
  • 2
    I disagree. The language design of Python is such that (a) it is often impossible to tell the type of a particular variable before instructions using it are executed, which means that an awful lot of optimizations that a typical c++ compiler would routinely perform are impossible for a python compiler, and (b) certain primitive operations that are common in python programs (such as modifying lists) are required by the language design to be atomic, which adds a very high overhead which is impossible to avoid for multithread implementations. This is a design issue. – Jules Dec 3 '14 at 8:31
  • Agreed, so I improved my answer. – Basile Starynkevitch Dec 3 '14 at 8:43
  • @BasileStarynkevitch Regarding your point about the C++ executable, it looks like Python can do the same: stackoverflow.com/questions/2933/… and stackoverflow.com/questions/5458048/… – warship Dec 4 '14 at 0:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.