Memory Layout and Performance
What you'll learn: How NumPy stores arrays in memory and why it matters for ML performance.
Why Memory Layout Matters
When you work with large datasets in machine learning, understanding how NumPy stores data in memory can dramatically speed up your operations—or slow them down if you're not careful.
C-Contiguous vs Fortran-Contiguous
NumPy arrays can be stored in two primary layouts:
- C-contiguous (row-major): Rows are stored together in memory. This is NumPy's default. Think of reading a book left-to-right, line by line.
- Fortran-contiguous (column-major): Columns are stored together. Like reading a newspaper column top-to-bottom, then moving to the next column.
Analogy: Imagine library books. C-contiguous arranges books by shelf (all shelf-1 books together), while Fortran-contiguous groups by topic across all shelves (all history books together, regardless of shelf).
Views vs Copies
When you slice or reshape arrays, NumPy sometimes creates a view (same data, different perspective) and sometimes a copy (duplicated data):
- Views share memory with the original—fast and memory-efficient
- Copies create new data—slower but independent
Operations like simple slicing create views, while boolean indexing usually creates copies.
Performance Implications
Accessing memory sequentially (following the storage order) is much faster than jumping around. Operating on C-contiguous arrays row-by-row is efficient; column-by-column access is slower. With million-row datasets, this difference becomes significant.
Key Takeaway: Understanding memory layout helps you write faster ML code—access data in its natural storage order, prefer views over copies when possible, and be mindful of which operations trigger memory duplication in large datasets.