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 .
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 ：
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 .
Python Optimization techniques ：https://wiki.python.org/moin/PythonSpeed/PerformanceTips
Scipy Class notes - senior Numpy：http://scipy-lectures.org/advanced/advanced_numpy/index.html#advanced-numpy
IPython Timing and analysis in ：https://pynash.org/2013/03/06/timing-and-profiling/