As a general rule, the smaller the data structure, the faster your computation.
Object-oriented languages can be especially problematic, as there might be many unused methods attached to your large data structures that need to be moved around with each loop iteration.
Overall guidance of computing optimization:
- Highlight any and all loops, especially nested loops: the higher the level of nesting, the slower they often are.
- Locate slow mathematical operations such as square roots, trigonometric functions, and so on, or anything that is not an addition or subtraction.
- Shrink the size of key data structures.
- Determine the precision ( oat, double, int, uint8, and so on) of often used variables.
- Avoid unnecessary calculations when possible.
- Attempt to simplify mathematical equations.
Additions and subtractions are faster than multiplications that are usually faster than division operations.
Unfortunately, the square root calculation itself is quite slow compared to basic multiplication, division, addition, subtraction, and logical comparisons. Even then, multiplications and divisions are usually significantly slower than your basic addition, subtraction, and logical comparisons.
In fact, often, an 8-bit integer will do, and sometimes an unsigned integer, which is incapable of expressing a negative value, is even better.
xrange
only allocates memory for the xrange
object and provides numbers on demand and as required. Also, xrange()
is implemented in pure C.
Use python -m cProfile
to check the time consumption of each method in a Python script.
The output from cProfile consists of plain text organized into five columns as follows:
ncalls
: This shows you how many times the function is calledtottime
: This shows you the time spent on the function, excluding the time spent on calling other functionspercall
: This shows you the total time, divided byncalls
cumtime
: This shows you the time spent on the function, including calls to other functionspercall
: This shows you thecumtime
value divided bytottime
One of the keys to NumPy’s performance is that each element of the array must be of the same data type.
- This homogeneity allows for very efficient memory packing and locality of reference.
- This is in stark contrast to Python lists, which can contain objects of varied data types and occupy different amounts of memory.