Python: IPython performance metrics

Machine vision 001 2020-11-13 02:46:27
python ipython performance metrics

Python：IPython Performance metrics

Sometimes you may need to compare the performance of two similar operations .

IPython Provides you with a magic command timeit To perform this operation . It runs the code many times to get more accurate results . Again , It also applies to measuring single line code .

for example , Do you know which of the following addition operations is better ？

x = 5;y = x**2;

x = 5;y = x*x;

x = np.uint8();y = x*x, perhaps y = np.square(x)?

We will be in IPython shell Pass through timeit Find out .

x = 5
%timeit y=x**2
# result
10000000 loops, best of 3: 73 ns per loop
%timeit y=x*x
# result
10000000 loops, best of 3: 58.3 ns per loop
z = np.uint8()
%timeit y=z*z
# result
1000000 loops, best of 3: 1.25 us per loop
%timeit y=np.square(z)
# result
1000000 loops, best of 3: 1.16 us per loop

You can see ,x = 5;y = x*x It's the fastest , It is better than Numpy About fast 20 times .

If you also consider the creation of arrays , It could be fast 100 times . cool , Right ?*(Numpy Developers are dealing with this problem )*

【 Be careful 】

Python Scalar operation ratio Numpy Fast scalar operation . therefore , For operations that contain one or two elements ,Python scalar than Numpy The array is good .

When the size of the array is slightly larger ,Numpy Have an advantage .

Let's give you another example . This time, , We will compare the same image of vc . countnonzero() and np.count_nonzero() Performance of .

%timeit z = cv.countNonZero(img)
# result
100000 loops, best of 3: 15.8 us per loop
%timeit z = np.count_nonzero(img)
# result
1000 loops, best of 3: 370 us per loop

see ,OpenCV Functional ratio Numpy The function is fast 25 times .

【 Be careful 】

Usually ,OpenCV Functional ratio Numpy Functions are faster . therefore , For the same operation , The first choice is OpenCV function .

however , There are exceptions. , Especially when Numpy When using views instead of copies .

There are other magical commands that measure performance 、 analysis 、 Line analysis 、 Memory measurement and so on .

They're all well documented . So here are only links to these documents . Interested readers can try .

Performance optimization technology

There are several techniques and coding methods that can make the most of Python and Numpy Performance of . Here's just the relevant information , And provide links to important information .

The main thing to pay attention to here is , First try to implement the algorithm in a simple way . Once it starts to work , Analyze it , And find the bottleneck , And optimize them .

The common skills are as follows ：

1. Try to avoid Python Use cycle in , Especially double / Triple cycle, etc . They are naturally slow .
2. The algorithm / Code is vectorized to the maximum extent possible , because Numpy and OpenCV The vector operation is optimized .
3. Using cache consistency .
4. Unless necessary , Don't copy arrays . Try using views instead of . Array copy is an expensive operation .

If your code is still slow after all this , Or the inevitable use of large loops , Then you can use Cython Wait for other libraries to speed up .