Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 49 ยท Performance and Internals

Memory Layout and Strides

Understand how NumPy stores arrays in memory: strides, C and Fortran order, contiguity flags, free transposes, sliding_window_view and safe use of as_strided.

A NumPy array looks like a grid, but the computer's memory is one long line of bytes. Somewhere between the two sits a small piece of bookkeeping that explains a lot of NumPy's behaviour: why a.T is instant, why slicing never copies, why reshape sometimes does, and why summing along one axis can be faster than along another. That bookkeeping is the strides tuple.

In this lesson you will look at how an array is laid out, read strides and contiguity flags, see why a transpose costs nothing, and use sliding_window_view to build rolling windows without copying data. You will also meet as_strided, the sharp tool behind those tricks, and learn why you should almost never call it yourself.

One block of memory, many views

An ndarray is made of two parts: a flat data buffer (one contiguous block of bytes) and a small header describing how to interpret it: the dtype, the shape, and the strides. A stride is the number of bytes you must skip in the buffer to move one step along an axis.

Take a 3 by 4 array of 4-byte integers stored in the default C order (also called row-major): the elements of row 0 come first, then row 1, then row 2. Moving one column right means moving 4 bytes (one element). Moving one row down means skipping a whole row, 4 elements, or 16 bytes. So the strides are (16, 4).

flowchart LR
    subgraph C["C order (row-major): strides (16, 4)"]
        direction LR
        c0["0"] --> c1["1"] --> c2["2"] --> c3["3"] --> c4["4"] --> c5["5"] --> c6["6"] --> c7["7"] --> c8["8"] --> c9["9"] --> c10["10"] --> c11["11"]
    end
    subgraph F["Fortran order (column-major): strides (4, 12)"]
        direction LR
        f0["0"] --> f1["4"] --> f2["8"] --> f3["1"] --> f4["5"] --> f5["9"] --> f6["2"] --> f7["6"] --> f8["10"] --> f9["3"] --> f10["7"] --> f11["11"]
    end

The same 3 by 4 array stored two ways. The numbers show which element sits at each position of the buffer.

In Fortran order (column-major, the layout used by Fortran, MATLAB and R) the columns are stored one after another, so moving down a column is the cheap step. The logical array is identical; only the order in memory differs.

Python Reading strides and contiguity flags

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The two arrays compare equal, because equality looks at the logical values. The flags show how they are stored, and ravel(order="K") reveals the buffer order. Most NumPy functions create C-ordered arrays. You get Fortran order from np.asfortranarray, from np.zeros(..., order="F"), and as the result of some transposes. See reshaping arrays for how order affects reshape and ravel.

Why a transpose is free

Because the strides describe the walk through memory, a transpose does not need to move any data. It only swaps the shape and the strides. The result is a view on the same buffer.

Python Transposes, slices and copies

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Read the output line by line:

  • t.strides is (4, 16), the reverse of a.strides. Nothing was copied, and t is Fortran-contiguous.
  • A slice with a step, a[:, ::2], doubles the column stride to 8 and is no longer C-contiguous. A slice with a negative step gives a negative stride, so the "reversed" array walks backwards through the same memory.
  • t.reshape(12) has to produce a flat run in C order, which cannot be described by a single stride over the original buffer, so NumPy makes a copy. This is why you can't always rely on reshape returning a view. Check views and copies for the full list of rules.
  • np.ascontiguousarray returns the array itself if it is already C-contiguous and a C-ordered copy otherwise.

Why layout affects speed

A modern CPU does not fetch single numbers from memory; it fetches a whole cache line (typically 64 bytes) at a time. When a loop walks through memory in order, every fetched line is fully used. When a loop jumps by a large stride, each fetch brings in 64 bytes of which only 8 are used, and the rest are wasted.

For a large C-ordered 2D array this means:

  • Operations that walk along rows (the last axis) touch neighbouring bytes and run at full memory speed.
  • Operations that walk down columns jump a whole row at a time and can be noticeably slower on big arrays. A reduction such as a.sum(axis=0) may be several times slower than a.sum(axis=1) once the array no longer fits in the CPU cache. NumPy tries to hide this by reordering its internal loops, so the gap is often smaller than you would fear, but it is real.
  • Copying a transposed array into a fresh C-ordered array (a.T.copy()) is much slower than copying a, because every write is contiguous but every read jumps.

The practical advice is simple. Keep the axis you loop or reduce over as the last axis of a C-ordered array, call np.ascontiguousarray once if you will reuse a transposed array many times, and measure before you worry. Timing is covered in writing fast NumPy code.

Sliding windows without copying

Many algorithms need every window of k consecutive items: moving averages, local maxima, pattern matching. A Python loop that slices each window is slow, and building a 2D array of windows by copying wastes memory, because most of the data is repeated.

Strides offer a trick: a window array can share the original buffer if each row starts one element after the previous row. NumPy exposes this safely as np.lib.stride_tricks.sliding_window_view.

Python sliding_window_view for rolling windows

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The window array has 6 rows but takes no extra memory for the data: both strides are 8 bytes, so row i simply starts one element later than row i-1. By default the view is read-only, which protects the original array from accidental writes through overlapping windows. The 2D version produces a grid of patches whose shape is (rows - 1, cols - 1, 2, 2), ready for reductions over the last two axes; that is the core of a convolution, which you will use in the image filter project.

One caution: the view is free, but the reduction is not. w.mean(axis=1) still reads every element k times. For a plain moving average, np.convolve or a cumulative sum is cheaper.

as_strided: the sharp tool

sliding_window_view is built on np.lib.stride_tricks.as_strided, which lets you give any shape and strides you like to an existing buffer.

Python
from numpy.lib.stride_tricks import as_strided

y = np.arange(6)
manual = as_strided(y, shape=(4, 3), strides=(y.strides[0], y.strides[0]))

This builds the same window array by hand. The danger is that as_strided performs no checks. If your shape and strides reach past the end of the buffer, NumPy happily reads whatever bytes are in memory next to it: garbage values, or a crash. If you write through an overlapping view, several positions alias the same element and the results are confusing. The NumPy documentation itself warns against using it unless you really need to.

Prefer sliding_window_view (checked, read-only by default) over as_strided. If you must use as_strided, pass writeable=False, compute the shape from the real array size, and test against a slow loop version.

Python Check as_strided against sliding_window_view

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Here the shape is computed from the array (len(y) - 2 windows of 3), so the view stays inside the buffer, and the result matches the safe function.

Common mistakes

  • Assuming reshape never copies. It returns a view when strides allow it and a copy otherwise. Do not rely on either.
  • Writing into a view by accident. t = a.T; t[0, 0] = 99 changes a as well.
  • Hard-coding byte strides. The numbers depend on the dtype; always compute them from itemsize or a.strides.
  • Using a transposed array in a hot loop without making it contiguous first.
  • Using as_strided with a wrong shape, which reads memory that does not belong to the array.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A C-ordered array has shape (3, 4) and dtype int32 (4 bytes per element). What are its strides?

    Moving one column skips one 4-byte element; moving one row skips a whole row of 4 elements, which is 16 bytes.
  2. 2. Why is a.T almost instant even for a huge array?

    A transpose only changes the header (shape and strides). No element is moved or copied.
  3. 3. What happens when you call reshape(-1) on a non-contiguous array such as a.T?

    A flat C-order walk over a transposed array cannot be described by a single stride over the original buffer, so NumPy copies.
  4. 4. Which is the safer way to build overlapping windows of a 1D array?

    sliding_window_view checks the window size against the array and returns a read-only view by default.
  5. 5. Why can summing a big C-ordered array along axis 0 be slower than along axis 1?

    Memory is fetched in cache lines. Large jumps use only a small part of each line, so more fetches are needed for the same work.

Summary

  • An array is a flat buffer plus a header with dtype, shape and strides; strides are byte steps per axis.
  • C order stores rows one after another ((16, 4) for a 3 by 4 int32 array); Fortran order stores columns one after another. Check a.flags for C_CONTIGUOUS and F_CONTIGUOUS.
  • Transposes, slices with steps and reversals only change shape and strides, so they are views and cost nothing.
  • reshape or ravel of a non-contiguous array may copy; np.ascontiguousarray makes a C-ordered array on demand.
  • Contiguous access is faster because of CPU cache lines; keep the axis you loop over last, and measure.
  • sliding_window_view gives rolling windows as a free read-only view; avoid raw as_strided unless you can prove the shape is safe.

Next: Writing Fast NumPy Code