opencv-python- study notes seven (program performance testing and upgrading)

1. Measurement Performance OpenCV

1.1 commonly used functions:

retval=cv.getTickCount()

retval=cv.getTickFrequency()

1.2 Fixed writing:

# Use GetTickCount () Time to GET 
E1 = cv.getTickCount () 
# CODE 
E2 = cv.getTickCount () 
Time = (E2 - E1) / cv.getTickFrequency () 

Print (Time) # result in seconds

 

# Use time.clock () timer 
Start = time.clock ()
 # CODE
 Elapsed = (time.clock () - Start) 
Print (Time) # result in seconds

 

The default 2.OpenCV Optimization

Many OpenCV functions use SSE2, AVX, etc. optimized. It also contains unencrypted code. So, if our system support these features, we should use them (almost all modern processors support them). It is enabled by default at compile time. Therefore, if enabled OpenCV optimized code, the code will run optimization, otherwise it will run non-optimized code. You can use cv.useoptimization () to check whether to enable / disable it and use cv.setuseoptimization () to enable / disable it.

 

Under normal circumstances OpenCV function faster than Numpy function. Therefore, for the same operation is preferably used in OpenCV function. There are exceptions, especially when used to view Numpy (not copied) to operate.

 

3. The detection efficiency in the Ipython

Sometimes you may need to compare two similar operating performance. IPython offers a magical command timeit to do this. It runs a low number code to get more accurate results. Likewise, they are also suitable for measuring single line of code.

 

Python scalar operations faster than Numpy scalar operations. Thus, for the operation comprises one or two elements, Python than Numpy scalar array. When the size of the array when it is slightly larger, Numpy will play a role.

 

We tried a case in point. This time, we will compare the same image cv.countNonZero () and np.count_nonzero () performance.

In [35]: %timeit z = cv.countNonZero(img)
100000 loops, best of 3: 15.8 us per loop
In [36]: %timeit z = np.count_nonzero(img)
1000 loops, best of 3: 370 us per loop

OpenCV function 25 times faster than Numpy function.

 

Usually, OpenCV function faster than Numpy function. Thus, for the same operation, the OpenCV function is preferred. However, there are exceptions, especially when Numpy use view instead of a copy.

 

4. Efficiency Optimization

  1. Try to avoid using loops, especially double-triple loop, they are inherently very slow.
  2. Algorithms make use of vector operations, because Numpy and OpenCV have been optimized for vector operations.
  3. The use of cache coherency.
  4. There is no need, then do not copy the array. Use view instead replication. Copies the array is very wasteful of resources.

 

Guess you like

Origin www.cnblogs.com/blog-xyy/p/11184357.html