/Curriculum/AdvancedMemory & Performance

38. NumPy Memory & PerformanceAdvanced

C-Order vs Fortran-Order, strides, memory flags, and CPU cache pre-fetching.

18 mins

Concept Overview

NumPy arrays can be ordered in row-major **C-Order** or column-major **Fortran-Order (F-Order)**. Traversing along contiguous strides maximizes CPU L1/L2 cache hit rates.

Hardware Mental Model

C-Order reads memory like English text (left-to-right along rows). F-Order reads memory vertically down columns. Iterating along the wrong order causes CPU cache misses.

Key Concepts (1)Click snippet to load in editor

Strides dictate how many bytes the memory pointer must advance to step to the next row or column.

flags = arr.flags; strides = arr.strides

⚠ Common Mistakes & Pitfalls

Avoid these frequent beginner syntax and logic traps
❌ Incorrect:transposed = arr.T; # Is now F-contiguous; looping row-wise causes cache misses
✓ Correct:contiguous = np.ascontiguousarray(arr.T)

Transposing flips strides and creates non-C-contiguous views. Use `np.ascontiguousarray()` to restore C-order.

Pro Tip: Use `np.ascontiguousarray(arr)` when passing arrays to C-extensions or Cython.

📌 Quick Revision

Core takeaway points from this topic
C-Order: Row-major (default in C and NumPy).
F-Order: Column-major (default in Fortran and MATLAB).
`arr.strides`: Tuple of bytes to step across each dimension.
`arr.flags`: Metadata on memory alignment, writeability, and contiguous layout.
Editor: 38. NumPy Memory & Performance
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state

🎯 Try It Yourself: Practice Challenge

Hands-on Mode

Solve the objective below using NumPy vectorized syntax

Objective:Inspect Strides of 2x3 Array

Create `arr = np.zeros((2, 3), dtype=np.int64)` and print `arr.strides`.

Expected Output Target:(24, 8)
Practice Workspace
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state