Does performance differ between Python or C ++ - OpenCV coding?

I try to start opencv a bit, but first I need to decide which OpenCV API is more useful. I predict that the Python implementation is shorter, but the runtime will be denser and slower than native C ++ implementations. Is there any information that can comment on the differences in performance and coding between the two perspectives?

+43
c ++ performance python opencv
Nov 17 '12 at 17:14
source share
2 answers

As mentioned in earlier answers, Python is slower than C ++ or C. Python is built for its simplicity, portability and, moreover, for creativity, when users only need to worry about their algorithm and not about programming problems.

But here, in OpenCV, there is something else. Python-OpenCV is just a wrapper around C / C ++ source code. It is commonly used to combine the best features of both languages, C / C ++ performance and Python simplicity .

Therefore, when you call a function in OpenCV from Python, what is actually being executed is the source of C / C ++. Thus, there will not be much difference in performance (I remember, I read somewhere that the penalty for performance is 1%, I don’t remember where. A rough estimate with some basic functions in OpenCV shows the worst case penalty <4% , t. e. penalty = [maximum time taken in Python - minimum time taken in C++]/minimum time taken in C++ ).

The problem occurs when your code has many native python codes. For example, if you create your own functions that are not available in OpenCV, things get worse. Such codes run in Python, which significantly reduces performance.

But the new OpenCV-Python interface has full Numpy support. Numpy is a package for scientific computing in Python. It is also a wrapper around native C code. It is a highly optimized library that supports a wide range of matrix operations, very suitable for image processing. Therefore, if you can properly combine OpenCV and Numpy functions, you will get very fast code.

Remember that always try to avoid loops and iterations in Python. Instead, use the array controls available in Numpy (and OpenCV). Simply adding two numpy arrays using C = A+B is much faster than using double loops.

For example, you can check out these articles:

+96
Nov 17 '12 at 18:12
source share

You're right, Python is almost always significantly slower than C ++, because it requires an interpreter that is not in C ++. However, this requires C ++ to be strongly typed, which leaves much less margin for error. Some people prefer strictly coding, while others take advantage of Python's inherent indulgence.

If you need a complete discourse on Python coding styles and C ++ styles, this is not the best place, try finding an article.

EDIT: Since Python is an interpreted language and C ++ is compiled to machine code, generally speaking , you can get performance benefits using C ++. However, regarding the use of OpenCV, the main OpenCV libraries are already compiled to machine code, so the Python shell around the OpenCV library executes the compiled code. In other words, when it comes to executing the computationally expensive OpenCV algorithms from Python, you won't see much of the performance since they are already compiled for the specific architecture you are working with.

+2
Nov 17 '12 at 17:18
source share



All Articles