-1

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?

3
  • 1
    Does this answer your question? Is Python Interpreted or Compiled? – gnat Mar 21 '20 at 12:15
  • As hand tuned performance experts spend more and more time playing with how these newer languages get transformed, performance is becoming less and less a good reason to choose any language. Performance stability used to be a good reason but more and more you're not the only thing on the CPU. – candied_orange Mar 21 '20 at 14:52
  • There are so many factors that effect performance I don't believe it's possible to give a generic answer to this question. For example, if your C++ app is reading/writing the disk and your Python app isn't, then I feel confident the Python app will be faster. Doesn't mean Python is faster in general--just means that particular situation. In general picking a language you're very proficient with and employing it well will lead to good fast code. If your code isn't performant enough then profile it. Picking a language based on "speed" is the definition of premature optimization. – Onorio Catenacci Mar 27 '20 at 15:03
6

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.

2

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.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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