Paul Manta
Paul Manta

Reputation: 31597

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

Upvotes: 11

Views: 1219

Answers (4)

Tony Delroy
Tony Delroy

Reputation: 106226

Keep it simple and tune performance as needed. The primary reason for embedding an interpreter in a C++ app is to allow run-time configuration/data to specify some processing - i.e. you can modify the script without recompiling the C++ program - that's your guide for when to call into the interpreter. Once in some interpreter call, the primary reasons to call back into C++ are:

  • to access or update some data that can't reasonably be exposed as a parameter to the call (or via some other registration process the interpreter supports)
  • to get better performance during some critical part of the processing

For the latter, try the script first (assuming it's as easy to develop there), then if it's slow identify where and how some C++ code might help. If/where performance does prove a problem - as a general guideline when calling from C++ to the interpreter or vice versa: try to line up as much work as possible then make the call into the other system. If you get stuck, come back to stackoverflow with a specific problem and actual code.

Upvotes: 2

Adam Wagner
Adam Wagner

Reputation: 16117

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

Upvotes: 9

Dan
Dan

Reputation: 10786

The cost is present but negligible. That's because you probably do a fair bit of work converting python's high level datatypes to C++-compatible representations. Of course this is similar to the cost of calling one C++ function from another, there's some overhead. The rules for when it's a good idea to switch from python to C++ are:

A function with few arguments

A function which does a large amount of processing on a small amount of data

A function which is called as rarely as possible - consolidate function calls if possible

Upvotes: 1

Preet Sangha
Preet Sangha

Reputation: 65536

The best metric should be something that wieghs up for you....

  • Makes development, debugging and testing easier (lowers dev cost)
  • Lowers the cost of maintenance
  • meets the performance requirement (provides solution)

Upvotes: 0

Related Questions