0
0
NumPydata~10 mins

Memory layout (C-order vs Fortran-order) in NumPy - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Memory layout (C-order vs Fortran-order)
Create 2D array
Choose memory order
C-order
Row-major
Memory stores rows
Memory stores columns
Access elements in memory order
Efficient memory use & speed
This flow shows how a 2D array is stored in memory depending on C-order (row-major) or Fortran-order (column-major), affecting access speed.
Execution Sample
NumPy
import numpy as np
arr_c = np.array([[1,2],[3,4]], order='C')
arr_f = np.array([[1,2],[3,4]], order='F')
print(arr_c.ravel())
print(arr_f.ravel())
Creates the same 2x2 array in C-order and Fortran-order, then prints their flattened memory layouts.
Execution Table
StepActionArray ShapeMemory OrderFlattened OutputExplanation
1Create arr_c with order='C'(2,2)C-order (row-major)[1 2 3 4]Rows stored sequentially: first row then second
2Create arr_f with order='F'(2,2)Fortran-order (column-major)[1 3 2 4]Columns stored sequentially: first column then second
3Print arr_c.ravel()(2,2)C-order[1 2 3 4]Flattened view matches row-major storage
4Print arr_f.ravel()(2,2)Fortran-order[1 3 2 4]Flattened view matches column-major storage
5End---All steps done, arrays created and flattened
💡 Execution stops after printing both flattened arrays showing different memory layouts.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
arr_cNone[[1 2] [3 4]] (C-order)[[1 2] [3 4]] (C-order)[[1 2] [3 4]] (C-order)[[1 2] [3 4]] (C-order)[[1 2] [3 4]] (C-order)
arr_fNoneNone[[1 2] [3 4]] (F-order)[[1 2] [3 4]] (F-order)[[1 2] [3 4]] (F-order)[[1 2] [3 4]] (F-order)
Key Moments - 3 Insights
Why does arr_f.ravel() output [1 3 2 4] instead of [1 2 3 4]?
Because arr_f uses Fortran-order, which stores columns sequentially in memory, so flattening reads down columns first, not rows.
Does the shape of arr_c and arr_f differ?
No, both arrays have the same shape (2,2), only their memory layout differs, as shown in the execution_table steps 1 and 2.
Why is memory layout important for performance?
Accessing elements in the order they are stored in memory (row-wise for C-order, column-wise for Fortran-order) is faster due to better cache usage, as implied in the concept_flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table row 2. What is the flattened output of arr_f?
A[1 3 2 4]
B[3 4 1 2]
C[1 2 3 4]
D[2 1 4 3]
💡 Hint
Check the 'Flattened Output' column in row 2 of execution_table.
At which step does arr_c get created with C-order memory layout?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Action' and 'Memory Order' columns in execution_table.
If we access arr_c elements column-wise, how does it affect performance?
APerformance improves because memory is stored column-wise
BPerformance decreases because memory is stored row-wise
CPerformance is unaffected
DPerformance is unpredictable
💡 Hint
Refer to concept_flow and key_moments about memory layout and access patterns.
Concept Snapshot
Memory layout controls how arrays are stored in memory.
C-order means row-major: rows stored one after another.
Fortran-order means column-major: columns stored sequentially.
Flattening (ravel) shows memory order.
Accessing data in stored order is faster.
Use order='C' or order='F' in numpy.array to set layout.
Full Transcript
This lesson shows how numpy arrays can be stored in memory in two ways: C-order (row-major) and Fortran-order (column-major). We create two identical 2x2 arrays but specify different memory orders. When flattened, the C-order array outputs elements row by row, while the Fortran-order array outputs elements column by column. This difference affects how data is accessed and can impact performance. Understanding memory layout helps write faster code by accessing elements in the order they are stored.