Question

I know that Python is an interpreted language and that c++ is a compiled one, or at least I like to think that I've understood some of their differences.

Although C++ is apparently faster than python, what if you compiled Python code to an exe: Would they be the same speed or will C++ still be much faster?

Was it helpful?

Solution

It completely depends what you want to compare.

Some Python compilers such as Cython generate C code which, when compiled will have the same performance than other compiled C or C++ code since IT IS in the end C code.

But let’s look closer at the issue:

  • python allows dynamic typing. The C translation of such constructs requires in some circumstances some additional overhead (hidden in Cython library calls). You would not have this overhead if implementing the same code natively in C. This could slow down compiled python executable compared to native C/C++ development.

  • there are however many real-world problems which would also require to additional overhead in C/C++ to cope with the dynamic conditions of the problem. For example, if you had a lot of dictionaries in your python code, you’d have to use lots of maps in C++. In this case the relative overhead of Cython would be compensated by additional overhead required in native development, and the performance of both languages could be very comparable.

  • the same is true when the python part is only a front end to tasks that will be performed bu GPU or other dedicated hardware chips. In this case the difference between python and C++ would be neglectible, because most of the work is performed elsewhere, and both languages are comparable when it comes to I/O and waiting for other processes ;-)

In the end it depends of the relative importance of these situations in your code.

My advice: If you’re comfortable with python, just continue to write in this language. If some performance benchmarks show that you have a measurable bottleneck, you could fine tune this part first in Python (most performance issues are in the algorithm, not in the compiler), or fine tune it in C/C+ (mixed development, if the performance issue is really linked to the language). A redevelopment in C++ would then be the last resort. If you’re familiar with C++ or intend to learn it for other reasons, the reasons of your choice will be different.

OTHER TIPS

I know that Python is an interpreted language and that c++ is a compiled one, or at least I like to think that I've understood some of their differences.

There is no such thing as an interpreted or a compiled language. Those terms are not even wrong, they are nonsensical.

Interpretation and compilation are traits of an interpreter or compiler (duh!), not a language.

Case in point: every currently existing Python implementation has at least one compiler. No existing Python implementation interprets Python, ever. OTOH, there are interpreters for C++.

Although C++ is apparently faster than python, what if you compiled Python code to an exe: Would they be the same speed or will C++ still be much faster?

That depends on the C++ code, the Python code, the C++ compiler, and the Python compiler.

If your code is badly written and uses sub-optimal algorithms, then it will be slow, no matter whether it is Python code or C++ code and whether it is compiled or not.

If your compiler does not optimize the code properly, then it will be slow, no matter whether it is a Python or C++ compiler.

Given two equally well implemented programs in two different languages and two equally well adapted execution environments, the performance will depend purely on the amount of money and resources poured into the compiler (particularly the optimizer). The language is irrelevant.

The Lisp and Smalltalk communities have figured out how to make dynamic languages fast decades ago, it's just that the communities of newer dynamic languages have chosen to ignore these results until very recently, and are now playing catch-up with other communities that have a 50 year head-start. (E.g. V8 was written by a couple of Smalltalk VM developers.)

Licensed under: CC-BY-SA with attribution
scroll top