Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Price of switching control between C++ and Python

I'm developing a C++ application that is extended/ scriptable with Python. Of course C++ is much faster than Python, in general, but does that necessarily mean that you should prefer to execute C++ code over Python code as often as possible?

I'm asking this because I'm not sure, is there any performance cost of switching control between code written in C++ and code written in Python? Should I use code written in C++ on every occasion, or should I avoid calling back to C++ for simple tasks because any speed gain you might have from executing C++ code is outmatched by the cost of switching between languages?

Edit: I should make this clear, I'm not asking this to actually solve a problem. I'm asking purely out of curiosity and it's something worth keeping in mind for the future. So I'm not interested in alternative solutions, I just want to know the answer, from a technical standpoint. :)

like image 244
Paul Manta Avatar asked Dec 30 '11 00:12

Paul Manta


People also ask

Which is more efficient C or Python?

C is a faster language compared to Python as it is compiled. Python programs are usually slower than C programs as they are interpreted. In C, the type of the various variables must be declared when they are created, and only values of those particular types must be assigned to them.

Which is better C C++ or Python?

C++ is faster than Python because it is statically typed, which leads to a faster compilation of code. Python is slower than C++, it supports dynamic typing, and it also uses the interpreter, which makes the process of compilation slower.

Is C++ and Python a good combination?

Trying to learn any programming language takes years of hard work and dedication. Trying to double that workload is likely to leave you burnt out and discouraged. However, if you do decide to learn two languages at the same time, C++ and Python are actually a great combination.


1 Answers

I don't know there is a concrete rule for this, but a general rule that many follow is to:

  • Prototype in python. This is quicker to write, and may be easier to read/reason about.
  • Once you have a prototype, you can now identify the slow portions that should be written in c++ (through profiling).
  • Depending on the domain of your code, the slow bits are usually isolated to the 'inner loop' types of code, so the number of switches between python an this code should be relatively small.
  • If your program is sufficiently fast, you've successfully avoided prematurely optimizing your code by writing too much in c++.
like image 97
Adam Wagner Avatar answered Oct 22 '22 06:10

Adam Wagner