# Enter the Metric In this article, we will have a look at performance improvements on a very basic level. A program can be written in many different ways, (hopefully) always obtaining the same result. Still, the execution time may differ drastically. In higher languages like Python, one may not tell instantly, how a predefined function is implemented. We will discuss this issue on the basis of the square function.

As my college Antonis mentioned in his article To Py or not to Py?, we are optimising a python CFD program. For increasing the performance of a program, one does not always need to switch to parallel methods. One can start with simple things, like choosing the right implementation of a function. An important function of this project is the distance function. It is used to quantify the difference between the two matrices. It is used to stop the iterative process when the output is merely changing and therefore a minimum is reached.

The distance function uses the Euclidian metric $d_{p,q} = \sqrt{\sum_{i}(p_i - q_i)^2}$. It calculates a scalar $d_{p,q}$ from the equally sized matrices $p$ and $q$. This function provides a lot of parts to investigate. In this article, we will only look at the $(\dots)^2$. You can investigate the rest of the function at home with the techniques provided.

To start we generate a random matrix $A$ of the size $m$

import numpy
A = numpy.random.rand(m)

The square $A^2$ can be now computed using a for-loop

for i in range(m):
A[i]*A[i]

or the numpy.power-function

numpy.power(A,2)

or the python power function

A**2

or a multiplication

A*A

to just name a few. In IPython a code snippet can be easily timed with

%timeit [expression]

I wrote a script to time the previous functions for different sizes of $A$, using the time-library. In the figure, the execution times of those implementations for different sizes of A are compared. 