My Coding >
Programming language >
Python >
Python libraries and packages >
Python NumPy >
In-place and out-place Numpy functions
In-place and out-place 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 in-place or out-place. What is the difference? Out-place functions are performed on data from one array and the results are stored in another array. In-place 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 in-place and out-place Bumpy functions. Out-place style codingLet's create an out-place 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 self-explanatory. 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. In-place style codingNow an in-place 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 out-place function takes 117.9 sec and the in-place function takes 95.3 sec to execute. So, the in-place function takes 1.23x less time than the out-place function for the same task. Almost a quarter! But the memory consumption looks more dramatic! An out-place function consumes a lot of memory on temporary arrays, rather than an in-place function never creates any temporary arrays. Therefore, I would like to conclude - if you can, you should always use in-lace 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 front-end output was performed with TemplateToolkit. |