0
0
NumPydata~5 mins

Contiguous memory layout concept in NumPy

Choose your learning style9 modes available
Introduction

Contiguous memory layout means data is stored in one continuous block in memory. This helps computers access data faster and makes operations quicker.

When you want to speed up calculations on large arrays.
When you need to share data efficiently between programs.
When you want to avoid extra memory use by copying data.
When working with libraries that require data in a continuous block.
When optimizing code for better performance.
Syntax
NumPy
import numpy as np

# Create a numpy array
array = np.array([1, 2, 3, 4])

# Check if array is contiguous in memory
is_contiguous = array.flags['C_CONTIGUOUS']

# Make array contiguous if not
contiguous_array = np.ascontiguousarray(array)

np.array creates an array with contiguous memory by default.

array.flags['C_CONTIGUOUS'] returns True if the array is stored contiguously in row-major order.

Examples
This array is created normally and is contiguous.
NumPy
import numpy as np

# Example 1: Contiguous array
array1 = np.array([10, 20, 30])
print(array1.flags['C_CONTIGUOUS'])  # True
Slicing with a step creates a view that is not contiguous.
NumPy
import numpy as np

# Example 2: Non-contiguous array by slicing
array2 = np.array([1, 2, 3, 4, 5])
sliced_array = array2[::2]
print(sliced_array.flags['C_CONTIGUOUS'])  # False
Using np.ascontiguousarray makes the array contiguous by copying data if needed.
NumPy
import numpy as np

# Example 3: Make non-contiguous array contiguous
array3 = np.array([1, 2, 3, 4, 5])
sliced_array = array3[::2]
contiguous_array = np.ascontiguousarray(sliced_array)
print(contiguous_array.flags['C_CONTIGUOUS'])  # True
Empty arrays are considered contiguous.
NumPy
import numpy as np

# Example 4: Empty array
empty_array = np.array([])
print(empty_array.flags['C_CONTIGUOUS'])  # True
Sample Program

This program shows how a normal array is contiguous, but its transpose is not. Then it makes the transpose contiguous using np.ascontiguousarray.

NumPy
import numpy as np

# Create a 2D array
original_array = np.array([[1, 2, 3], [4, 5, 6]])
print("Original array:")
print(original_array)
print("Is original array contiguous?", original_array.flags['C_CONTIGUOUS'])

# Create a transposed view (non-contiguous)
transposed_array = original_array.T
print("\nTransposed array:")
print(transposed_array)
print("Is transposed array contiguous?", transposed_array.flags['C_CONTIGUOUS'])

# Make transposed array contiguous
contiguous_transposed = np.ascontiguousarray(transposed_array)
print("\nContiguous transposed array:")
print(contiguous_transposed)
print("Is contiguous transposed array contiguous?", contiguous_transposed.flags['C_CONTIGUOUS'])
OutputSuccess
Important Notes

Checking if an array is contiguous is fast and helps optimize code.

Making an array contiguous may copy data, which uses extra memory.

Contiguous arrays allow faster access because data is stored in one block.

Summary

Contiguous memory layout means data is stored in one continuous block.

Not all numpy arrays are contiguous, especially views like slices or transposes.

You can check and convert arrays to contiguous to improve performance.