Practical point of view: why do I want to use Python with C ++?

I have seen some examples of using Python with C ++, and I'm trying to figure out why someone wants to do this. What are the benefits of invoking C ++ code from an external language such as Python?

I would appreciate a simple example: Boost :: Python will do

+4
source share
7 answers

It depends on your point of view:

Calling C ++ code from a python application

You usually want to do this when performance is a problem. Highly dynamic languages ​​such as python are usually somewhat slower than native code such as C ++. C ++ “features”, such as manual memory management, allow you to develop very fast libraries that can then be called from python to improve performance.

Another reason is that most libraries on both windows and * nix are written in C or C ++, and this is a huge advantage for having this existing code base.

Calling python code from a C ++ application

Complex applications sometimes require the ability to define additional capabilities. Adding behavior to a compiled application is useless, requires source code, and takes a lot of time. Therefore, it is often strategic to implement a scripting language such as python to make the application more flexible and customizable.

As for the example: I think you need to clarify a bit what you are interested in if you want the sample to be useful. Acceleration Guide provides a simple hello world-wide sample if that's what you are looking for.

+21
source

Generally, you should call C ++ from python in order to use an existing library or other functions. Often someone else wrote a set of functions to make your life easier, and calling compiled C code is easier than rewriting a library in python.

Another reason is for performance purposes. Often, the specific functions of a fully written program are written in a precompiled language such as C because they take a long time to execute and can be more efficiently executed in a lower level language.

The third reason is the interaction with devices. Python does not contain much code for working with sound cards, serial ports, etc. If your device needs a device driver, python will talk to it using the pre-compiled code that you include in your application.

+5
source

There are two possibilities:

  • Perhaps C ++ code is already written and available for use.
  • C ++ code probably faster / less than Python equivalent
+3
source

Since C ++ provides a direct way to call OS services and (if used carefully) it can create more efficient code in memory and time, while Python is a high-level language and less painful for use in situations where full efficiency is not a problem, and where you already have libraries that give you access to the services you need.

If you are a C ++ user , you may wonder why this is necessary, but the expressiveness and security of a high-level language have such a huge relative impact on your performance, it must be experienced in order to be understood or believed.

I can’t speak specifically for Python, but I heard people talk about tripling their productivity, performing most of their development in it and using C ++ only where necessary, by profiling or creating additional libraries.

If you are a Python user , you may not have encountered a situation where you need something outside the existing libraries, and you may not have a performance problem that you get from pure Python (this is likely). In this case - you are lucky! You can forget about all this.

+3
source

Here is a real life example: I wrote a DLL in C for interacting with some special hardware to work. Then, at the first stage of testing, I wrote short C programs to make sure that different commands work correctly. The process of writing, compiling and starting was probably 3-5 times as long as I finally wrote the Python interface for the DLL using ctypes .

Now I can write test scripts much faster, without paying attention to the correct initialization of variables and memory management, which I would have to worry about in C. In fact, I could even use modular test libraries in Python to create much more reliable tests than before. Would it be possible in C? Absolutely, but it would take me a lot longer, and it would be much more lines of code.

Fewer lines of code in Python mean (in general) that in my core logic there are fewer things that could go wrong.

Moreover, since the hardware connection is almost completely tied to IO, there is no need to write any supporting code in C. I can also program in what is developing fastest.

So you go, a real example.

+2
source
  • Performance:

From my limited experience, Python is about 10 times slower than C. Using Psyco will greatly improve it, but still about 5 times slower than C. BUT, calling a c-module from python is only slightly faster than Psyco.

  1. When you have libraries in C.
    For example, I work hard on SIP. These are very complex protocol stacks, and there is no full Python implementation. So my only choice is to call SIP libraries written in C.

There are also cases such as video / audio decoding.

0
source

One of the nice things about using the scripting language is that you can reload the new code into the application without leaving the application, and then make changes, recompile and restart the application. When people talk about faster development times, some of them relate to this opportunity. The disadvantage of using scripting languages ​​is that their debuggers are usually not as complete as yours in C ++. I have not programmed Python, so I do not know what functions of its debugger, if any.

This answer doesn’t exactly answer what you asked for, but I thought it mattered. The answer is more pro / minus using scripting language. Please don't flame me. :)

0
source

All Articles