Finally, the fact that items are stored contiguously in memory allows NumPy to take advantage of vectorized instructions of modern CPUs, such as Intel's SSE and AVX, AMD's XOP, and so on.Adjacent items are then loaded very efficiently ( sequential locality, or locality of reference). Indeed, the cache loads bytes in chunks from RAM to the CPU registers. Spatial locality in memory access patterns results in performance gains notably due to the CPU cache.There would be a significant overhead to do that in Python with a list. Knowing the address of the memory block and the data type, it is just simple arithmetic to loop over all items, for example. Computations on arrays can be written very efficiently in a low-level language such as C (and a large part of NumPy is actually written in C).Why is this so important? Here are the main reasons: This aspect is the critical feature that makes NumPy arrays so efficient. This is the main difference between an array and a pure Python structure, such as a list, where the items are scattered across the system memory. This block of memory is called the data buffer. The data is stored in a homogeneous and contiguous block of memory, at a particular address in system memory ( Random Access Memory, or RAM). Why are NumPy arrays efficient?Ī NumPy array is basically described by metadata (notably the number of dimensions, the shape, and the data type) and the actual data. In this section, we will see what happens under the hood when using NumPy, and how this knowledge allows us to understand the tricks given in this recipe. Let's define a function aid() that returns the memory location of the underlying data buffer:ħ84 µs ± 2.39 µs per loop (mean ± std. Getting readyįirst, we need a way to check whether two arrays share the same underlying data buffer in memory. In that respect, we will need to dig into the internals of NumPy. In this recipe, we will see how to avoid unnecessary array copies in order to save memory. In the next few recipes, we will see some tricks that can help us write optimized NumPy code. However, it is also possible to unintentionally write non-optimized code with NumPy. We can achieve significant performance speedups with NumPy over native Python code, particularly when our computations follow the Single Instruction, Multiple Data (SIMD) paradigm. ▶ Go to Chapter 4 : Profiling and Optimization ▶ Text on GitHub with a CC-BY-NC-ND license The ebook and printed book are available for purchase at Packt Publishing. This is one of the 100+ free recipes of the IPython Cookbook, Second Edition, by Cyrille Rossant, a guide to numerical computing and data science in the Jupyter Notebook. Understanding the internals of NumPy to avoid unnecessary array copying
0 Comments
Leave a Reply. |