Memory Layout and Contiguity
NumPy arrays store data in memory and use shape, strides, and data types to determine how elements are accessed. Understanding memory layout and contiguity helps you work efficiently with large arrays, slicing, reshaping, transposing, and numerical computations. In this lesson, you will learn C-order and Fortran-order layouts, array strides, contiguous arrays, memory flags, and practical optimization techniques.
1. What Is Memory Layout?
Memory layout describes how the elements of an array are arranged or addressed in computer memory. NumPy arrays have a logical shape, but their elements are accessed according to their underlying data storage, strides, and data type.
Consider this two-dimensional array:
import numpy as np
arr = np.array([
[10, 20, 30],
[40, 50, 60]
])
print(arr)
Output
[[10 20 30] [40 50 60]]
The array has two rows and three columns. Its shape is
(2, 3). However, its logical row-and-column arrangement
does not by itself tell you how its elements are addressed in memory.
Important Concepts
- Shape: The number of elements along each dimension.
- Data type: The type and size of each array element.
- Strides: The number of bytes to move in memory when an index increases along an axis.
- Contiguity: Whether the array follows a specified continuous memory layout.
2. C-Order Memory Layout
C-order, also called row-major order, stores or traverses the last axis fastest. For a two-dimensional array, elements in each row are visited before moving to the next row.
import numpy as np
arr = np.array([
[10, 20, 30],
[40, 50, 60]
])
print(arr.ravel(order="C"))
Output
[10 20 30 40 50 60]
C-order traversal processes each row from left to right.
You can request C-order when creating or converting arrays and when flattening or reshaping data.
arr = np.array([
[10, 20, 30],
[40, 50, 60]
])
flat_c = np.ravel(arr, order="C")
print(flat_c)
Output
[10 20 30 40 50 60]
3. Fortran-Order Memory Layout
Fortran-order, also called column-major order, traverses the first axis fastest. For a two-dimensional array, it visits the elements of each column before moving to the next column.
import numpy as np
arr = np.array([
[10, 20, 30],
[40, 50, 60]
])
print(arr.ravel(order="F"))
Output
[10 40 20 50 30 60]
Fortran-order traversal processes each column from top to bottom.
The order="F" argument requests Fortran-style traversal.
In functions such as ravel(), this specifies the order in
which values are read; it does not automatically mean that the original
array is physically stored in Fortran-contiguous memory.
arr = np.array([
[10, 20, 30],
[40, 50, 60]
])
flat_f = np.ravel(arr, order="F")
print(flat_f)
Output
[10 40 20 50 30 60]
4. What Is Array Contiguity?
An array is contiguous in a particular order when its elements occupy a compatible continuous memory layout for that order, taking the array's shape, strides, and element size into account.
NumPy exposes two commonly used flags:
C-contiguous
The array satisfies NumPy's C-contiguity requirements. For two-dimensional arrays, this commonly corresponds to rows being stored consecutively.
Fortran-contiguous
The array satisfies NumPy's Fortran-contiguity requirements. For two-dimensional arrays, this commonly corresponds to columns being stored consecutively.
import numpy as np
arr = np.array([
[1, 2, 3],
[4, 5, 6]
])
print(arr.flags.c_contiguous)
print(arr.flags.f_contiguous)
Output
True False
This array is C-contiguous but not Fortran-contiguous.
5. Inspecting NumPy Array Flags
The flags attribute provides information about the array's
memory properties. It helps you inspect contiguity and whether an array
owns its data.
import numpy as np
arr = np.array([
[1, 2, 3],
[4, 5, 6]
])
print(arr.flags)
The output includes properties such as:
| Flag | Meaning |
|---|---|
C_CONTIGUOUS |
Indicates whether the array is C-contiguous. |
F_CONTIGUOUS |
Indicates whether the array is Fortran-contiguous. |
OWNDATA |
Indicates whether the array owns its element data. |
WRITEABLE |
Indicates whether the array's elements can be modified. |
ALIGNED |
Indicates whether the array data satisfies the alignment requirements for its data type. |
Inspect Individual Flags
import numpy as np
arr = np.arange(12).reshape(3, 4)
print("C-contiguous:", arr.flags.c_contiguous)
print("F-contiguous:", arr.flags.f_contiguous)
print("Owns data:", arr.flags.owndata)
print("Writeable:", arr.flags.writeable)
Output
C-contiguous: True F-contiguous: False Owns data: False Writeable: True
The reshaped array is a view of the array created by
np.arange(12) in this example. Therefore, it is not the
owner of the underlying data, even though it is C-contiguous.
6. Understanding Array Strides
Strides tell NumPy how many bytes to move in memory when moving one position along each axis. The number of stride values equals the number of dimensions in the array.
For a typical C-contiguous two-dimensional array with 64-bit integers, each element occupies 8 bytes. Moving one column requires 8 bytes, while moving one row requires enough bytes to pass across all columns.
import numpy as np
arr = np.array([
[10, 20, 30],
[40, 50, 60]
], dtype=np.int64)
print("Shape:", arr.shape)
print("Item size:", arr.itemsize)
print("Strides:", arr.strides)
Output
Shape: (2, 3) Item size: 8 Strides: (24, 8)
Here, the stride is 24 bytes along the row axis and 8 bytes along the column axis. This is because each row contains three 8-byte elements.
Stride Calculation
For this C-contiguous array:
- Element size = 8 bytes.
- Number of columns = 3.
- Row stride = 3 × 8 = 24 bytes.
- Column stride = 8 bytes.
Inspect Strides of Different Data Types
import numpy as np
arr_int32 = np.array([10, 20, 30], dtype=np.int32)
arr_int64 = np.array([10, 20, 30], dtype=np.int64)
print("int32 item size:", arr_int32.itemsize)
print("int32 strides:", arr_int32.strides)
print("int64 item size:", arr_int64.itemsize)
print("int64 strides:", arr_int64.strides)
Typical Output
int32 item size: 4 int32 strides: (4,) int64 item size: 8 int64 strides: (8,)
A one-dimensional contiguous array generally has a stride equal to its element size. The exact element size depends on the selected data type.
7. How Slicing Changes Strides
Basic slicing can create a view that uses the same underlying data but has different strides. This is one reason a sliced array may not be contiguous.
import numpy as np
arr = np.arange(12, dtype=np.int64).reshape(3, 4)
print("Original strides:", arr.strides)
sliced = arr[:, ::2]
print("Sliced array:")
print(sliced)
print("Sliced strides:", sliced.strides)
print("C-contiguous:", sliced.flags.c_contiguous)
Output
Original strides: (32, 8) Sliced array: [[ 0 2] [ 4 6] [ 8 10]] Sliced strides: (32, 16) C-contiguous: False
The slice selects every second column. Its column stride becomes 16 bytes because the next selected element is two 8-byte elements away.
8. Transpose and Memory Layout
Transposing a two-dimensional NumPy array swaps its axes. NumPy can often represent the transposed result by changing its shape and strides, without copying all its element data.
import numpy as np
arr = np.arange(6, dtype=np.int64).reshape(2, 3)
transposed = arr.T
print("Original:")
print(arr)
print("Transposed:")
print(transposed)
print("Original strides:", arr.strides)
print("Transposed strides:", transposed.strides)
print("Original C-contiguous:", arr.flags.c_contiguous)
print("Transposed C-contiguous:", transposed.flags.c_contiguous)
print("Transposed F-contiguous:", transposed.flags.f_contiguous)
Output
Original: [[0 1 2] [3 4 5]] Transposed: [[0 3] [1 4] [2 5]] Original strides: (24, 8) Transposed strides: (8, 24) Original C-contiguous: True Transposed C-contiguous: False Transposed F-contiguous: True
For this array, transposition swaps the stride order. The transposed array is Fortran-contiguous, and the operation can be represented as a view of the original data.
Check Memory Sharing
print(np.shares_memory(arr, transposed))
Output
True
The original and transposed arrays share element memory. Modifying a corresponding element through the transposed view can therefore affect the original array.
9. Creating C-Contiguous and Fortran-Contiguous Arrays
NumPy provides functions to create arrays with a requested memory layout. Use these when an algorithm, numerical library, or data-processing pipeline benefits from a specific layout.
Create a C-Contiguous Array
import numpy as np
arr = np.ascontiguousarray(
np.array([[1, 2, 3], [4, 5, 6]])
)
print(arr.flags.c_contiguous)
print(arr)
Output
True [[1 2 3] [4 5 6]]
np.ascontiguousarray() returns a C-contiguous array. It
may return the original array if it already satisfies the requirement,
or create a copy if needed.
Create a Fortran-Contiguous Array
import numpy as np
arr = np.asfortranarray(
np.array([[1, 2, 3], [4, 5, 6]])
)
print(arr.flags.f_contiguous)
print(arr.flags.c_contiguous)
print(arr)
Output
True False [[1 2 3] [4 5 6]]
np.asfortranarray() returns a Fortran-contiguous array.
For this two-dimensional example, the resulting array is Fortran-contiguous
and not C-contiguous.
10. Making a Non-Contiguous Array Contiguous
Suppose you select every second column from an array. The result may
be non-contiguous. If a downstream operation specifically requires
contiguous data, you can use np.ascontiguousarray().
import numpy as np
original = np.arange(12).reshape(3, 4)
sliced = original[:, ::2]
contiguous = np.ascontiguousarray(sliced)
print("Sliced:")
print(sliced)
print("Sliced C-contiguous:",
sliced.flags.c_contiguous)
print("Converted C-contiguous:",
contiguous.flags.c_contiguous)
print("Shares memory:",
np.shares_memory(sliced, contiguous))
Output
Sliced: [[ 0 2] [ 4 6] [ 8 10]] Sliced C-contiguous: False Converted C-contiguous: True Shares memory: False
In this example, the slice is non-contiguous, so
np.ascontiguousarray() creates a separate contiguous array.
The converted array contains the same values but has its own element data.
11. Flattening, Raveling, and Memory Layout
NumPy provides flatten() and ravel() to produce
one-dimensional arrays. They differ in how they handle copying.
| Method | Behavior | Memory Consideration |
|---|---|---|
arr.flatten() |
Returns a flattened copy. | Creates independent element data. |
arr.ravel() |
Returns a flattened array. | Returns a view when possible; otherwise, it copies. |
np.reshape(arr, shape) |
Changes the array shape. | Returns a view when possible; otherwise, it may copy. |
Compare flatten() and ravel()
import numpy as np
arr = np.array([
[1, 2, 3],
[4, 5, 6]
])
flat_copy = arr.flatten()
flat_ravel = arr.ravel()
flat_copy[0] = 100
flat_ravel[1] = 200
print("Original:")
print(arr)
print("Flatten copy:")
print(flat_copy)
print("Ravel result:")
print(flat_ravel)
Output
Original: [[ 1 200 3] [ 4 5 6]] Flatten copy: [100 2 3 4 5 6] Ravel result: [ 1 200 3 4 5 6]
For this C-contiguous array, ravel() can return a view.
Changing the second element of the raveled result updates the original.
The flattened copy remains independent.
flatten() when
you explicitly need a copy. Choose ravel() when a flattened
result is needed and a view is acceptable.
12. Memory Layout and Performance
Contiguous arrays can be useful for efficient iteration, cache behavior, and compatibility with libraries that expect a particular layout. However, contiguity alone does not determine performance. The algorithm, array size, data type, access pattern, and numerical library also matter.
Why Memory Layout Can Matter
- Sequential access can make effective use of CPU cache lines.
- Some compiled functions and external libraries require contiguous inputs.
- Strided arrays can skip through memory and may be less efficient for particular access patterns.
- Creating a contiguous copy consumes time and additional memory.
- Vectorized NumPy operations can still perform well on many non-contiguous arrays.
Example: Inspect Before Optimizing
import numpy as np
data = np.arange(1_000_000).reshape(1000, 1000)
column_slice = data[:, ::2]
print("Original C-contiguous:",
data.flags.c_contiguous)
print("Slice C-contiguous:",
column_slice.flags.c_contiguous)
print("Slice shape:",
column_slice.shape)
Output
Original C-contiguous: True Slice C-contiguous: False Slice shape: (1000, 500)
The slice skips every other column, so it is not C-contiguous in this example. You should only create a contiguous copy when it benefits the operations that follow or is required by the receiving API.
13. Practical Example: Student Marks Analysis
Consider marks for Samadhan, Pradnya, and Samiksha. We will inspect the array layout, create a sliced view, and make a contiguous copy when needed.
import numpy as np
student_names = np.array([
"Samadhan",
"Pradnya",
"Samiksha"
])
marks = np.array([
[78, 82, 88, 91],
[85, 90, 87, 93],
[92, 95, 89, 97]
], dtype=np.int64)
print("Student names:", student_names)
print("Marks:")
print(marks)
print("Shape:", marks.shape)
print("Data type:", marks.dtype)
print("Item size:", marks.itemsize)
print("Strides:", marks.strides)
print("C-contiguous:", marks.flags.c_contiguous)
# Select every second subject
selected_marks = marks[:, ::2]
print("Selected marks:")
print(selected_marks)
print("Selected C-contiguous:",
selected_marks.flags.c_contiguous)
# Prepare a contiguous independent array
prepared_marks = np.ascontiguousarray(selected_marks)
print("Prepared C-contiguous:",
prepared_marks.flags.c_contiguous)
print("Shares memory:",
np.shares_memory(marks, prepared_marks))
Expected Output
Student names: ['Samadhan' 'Pradnya' 'Samiksha'] Marks: [[78 82 88 91] [85 90 87 93] [92 95 89 97]] Shape: (3, 4) Data type: int64 Item size: 8 Strides: (32, 8) C-contiguous: True Selected marks: [[78 88] [85 87] [92 89]] Selected C-contiguous: False Prepared C-contiguous: True Shares memory: False
The original marks array is C-contiguous. Selecting every second
subject produces a non-contiguous view. Converting that selection
with np.ascontiguousarray() creates independent,
contiguous data in this example.
This technique is useful when preparing data for a function that requires contiguous input while preserving the original student records.
14. Common Mistakes
Mistake 1: Assuming Every Array Is C-Contiguous
Slicing with steps, transposing, and other operations can produce
arrays that are not C-contiguous. Check
arr.flags.c_contiguous when the layout matters.
Mistake 2: Confusing Traversal Order with Physical Storage
ravel(order="F") requests column-major traversal.
It does not guarantee that the source array is physically
Fortran-contiguous.
Mistake 3: Assuming Transpose Always Copies Data
NumPy can often transpose an array by changing its shape and strides.
Use np.shares_memory() to check whether the result
shares element memory with the source.
Mistake 4: Copying Every Non-Contiguous Array
Many NumPy operations support strided arrays directly. Convert to contiguous storage only when needed for compatibility or when measurements show a benefit.
15. Practice Exercises
Exercise 1: C-Order and Fortran-Order
Create a 3 by 3 array containing values from 1 to 9. Print
ravel(order="C") and ravel(order="F").
Compare their element orders.
Exercise 2: Inspect Contiguity
Create a 4 by 4 array and print its C-contiguous and Fortran-contiguous flags.
Exercise 3: Understand Strides
Create a two-dimensional int64 array. Print
shape, itemsize, and
strides. Calculate the expected row and column strides.
Exercise 4: Slice and Transpose
Slice every second column from an array, then transpose it. Compare the shapes, strides, and contiguity flags of both results.
Exercise 5: Make an Array Contiguous
Create a non-contiguous slice and convert it using
np.ascontiguousarray(). Verify the resulting flags
and check memory sharing.
Exercise 6: Student Marks
Create a marks matrix for Samadhan, Pradnya, and Samiksha. Select every second subject, inspect the selected array's contiguity, and create a contiguous version.
Summary
NumPy memory layout determines how array elements are addressed and
traversed. C-order processes the last axis fastest, while Fortran-order
processes the first axis fastest. The shape,
dtype, itemsize, and strides
attributes help explain how an array is organized.
C-contiguous and Fortran-contiguous flags describe whether an array satisfies the corresponding memory-layout requirements. Slicing and transposing can change strides and produce non-contiguous views without necessarily copying the underlying data.
Use arr.flags to inspect memory properties,
np.ascontiguousarray() to obtain C-contiguous data, and
np.asfortranarray() to obtain Fortran-contiguous data.
Avoid unnecessary copies, and optimize memory layout according to
your actual processing requirements.