My Coding >
Programming language >
Python >
Python libraries and packages >
Python NumPy >
Inplace and outplace Numpy functions
Inplace and outplace Numpy functionsTo apply the same operation on all values in the Numpy array we can apply functions over the Numpy array at once. This operation is called a vectorized operation. But this operation can be performed inplace or outplace. What is the difference? Outplace functions are performed on data from one array and the results are stored in another array. Inplace operations are performed over the data in the array and the results are stored in the same array. To understand the difference between these two types of functions, let's calculate the same mathematical expression in two different ways and let's apply it to a very large numpy array. \[ B = \sin(\sin(\sin(\sin(\sin(\sin(\sin(\sin((A^A)^{(A^A)})))))))) \] This function is not very sensefull, but nice for explaining the idea of inplace and outplace Bumpy functions. Outplace style codingLet's create an outplace style function for numpy calculations. We have numpy array frm and we will apply the described above function on it and store the final result into an array to
It is easy to write and the code looks simple and selfexplanatory. But how this code is working? This is a big issue  for every step, Python creates a temporary array, the same size as tmp and stores calculations there, and repeats it for every function. Inplace style codingNow an inplace function. First of all, we need to copy frm array to a to array and then start to perform calculation with it:
The code looks much longer and more complicated, but in fact, this code is more optimized, because, during its execution, no temporary arrays in the memory will be produced. Let's compare them on a large array. After each operation I do perform sleeping for 5 seconds, to observe the memory changes with outside monitoring tools. The array for calculation will be 25_000x25_000 elements.
This code will be executed with the following command and then the results will be observed with the next command::
First of all, the execution of this code reveals, that the outplace function takes 117.9 sec and the inplace function takes 95.3 sec to execute. So, the inplace function takes 1.23x less time than the outplace function for the same task. Almost a quarter! But the memory consumption looks more dramatic! An outplace function consumes a lot of memory on temporary arrays, rather than an inplace function never creates any temporary arrays. Therefore, I would like to conclude  if you can, you should always use inlace Numpy functions!

Last 10 artitles
9 popular artitles


© 2020 MyCoding.uk My blog about coding and further learning. This blog was writen with pure Perl and frontend output was performed with TemplateToolkit. 