Performance Optimization Techniques
There are several techniques and coding methods to exploit maximum performance of Python and Numpy. Only relevant ones are noted here and links are given to important sources. The main thing to be noted here is that, first try to implement the algorithm in a simple manner. Once it is working, profile it, find the bottlenecks and optimize them.
- Avoid using loops in Python as far as possible, especially double/triple loops etc. They are inherently slow.
- Vectorize the algorithm/code to the maximum possible extent because Numpy and OpenCV are optimized for vector operations.
- Exploit the cache coherence.
- Never make copies of array unless it is needed. Try to use views instead. Array copying is a costly operation.
Even after doing all these operations, if your code is still slow, or use of large loops are inevitable, use additional libraries like Cython to make it faster.
Reference :
沒有留言:
張貼留言