Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 45 ยท Vectorization and Performance

Performance and Memory

Speed up MATLAB and Octave code: preallocate arrays, time with tic and toc, use the profiler, respect column-major order, avoid growth, and know when to go parallel.

Most MATLAB and Octave scripts are fast enough the way you first write them. When one is not, guessing is the slowest way to fix it. The reliable recipe has three steps: measure to find the slow part, change one thing, measure again. This lesson gives you the measuring tools and the handful of habits that explain most real slowdowns.

Real MATLAB/Octave code is read-only on this site. The TryIts are NumPy stand-ins demonstrating the same ideas (growing versus preallocating, memory order); they print comparisons rather than raw times because timings vary by machine.

The speed-up loop

Measure tic/toc or the profiler.
Find Fix the slowest line, not all lines.
Change One change at a time.
Re-measure Keep the change only if it helps.
1 / 2

Measuring with tic, toc and timeit

tic starts a stopwatch and toc reads it:

MATLAB
tic
A = rand(2000);
B = A * A;
elapsed = toc;          % seconds as a number
fprintf('Multiply took %.3f s\n', elapsed);

A single run is noisy: the first call may include loading code or warming caches. MATLAB's timeit(@() f(x)) runs a function several times and returns a reliable typical time; in Octave you can loop yourself and average. For very short operations, repeat them inside the timed section.

To measure a whole script at once, use the profiler:

MATLAB
profile on
myanalysis
profile viewer          % table of time per function and per line

(Octave offers profile on, profile off and profshow.) Sort by self time and look at the top few lines. Often one or two lines account for most of the total, and those are the only ones worth changing.

Preallocate instead of growing

Assigning beyond the end of an array makes MATLAB allocate a bigger block and copy everything. Do that thousands of times and the copying dominates.

MATLAB
% Slow: the array grows each pass
out = [];
for k = 1:100000
    out(end+1) = k^2;
end

% Fast: allocate once
out = zeros(1, 100000);
for k = 1:100000
    out(k) = k^2;
end

The editor underlines the first form with the hint that the variable changes size on every iteration. Preallocate with zeros, ones, nan, cell(n, 1) or strings(n, 1); use nan when "no value yet" should be visible. If you cannot know the final size, allocate a generous block and trim at the end with out = out(1:count), or collect results in chunks. For the construction functions see Ranges and Special Matrices.

Python Growing versus preallocating

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Note that NumPy's np.append is an honest stand-in for the cost of growth: it copies the whole array each call. MATLAB's own growth behaves similarly for large arrays, though recent versions soften the penalty for small cases.

Column-major order

MATLAB and Octave store a matrix column by column in memory. Walking down a column touches neighboring memory cells; walking along a row jumps by a whole column length each step, which is slower for big matrices because the processor cache works best with sequential reads.

MATLAB
A = rand(5000);
total = 0;

% Better: the inner loop runs down a column
for j = 1:size(A, 2)
    for i = 1:size(A, 1)
        total = total + A(i, j);
    end
end

Put the first index in the innermost loop. This is the reverse of C and NumPy, which are row-major by default. Better still, avoid the loop: sum(A(:)) reads memory in the ideal order. Linear indexing (A(7)) also follows column order, as shown in Indexing and Slicing.

Python Memory order matters

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The strides printed show the layout directly: in F order, moving down a column steps by 8 bytes (one double), while moving along a row steps by 8 times the column length. The program does not claim a winner in numbers because NumPy's reductions hide much of the difference, but in hand-written loops the effect is real.

In-place work and memory

  • Operations such as A = A + 1 inside a function can reuse memory, so writing to the same variable is better than creating B when you do not need the old value.
  • MATLAB passes arguments by value, but only copies if the function modifies its input (copy-on-write). Modifying a large array inside a function therefore doubles its memory use temporarily.
  • Large intermediates, such as (A*B)*C on huge matrices, can exhaust memory. Check sizes with whos and free what you no longer need with clear bigVariable.
  • Smaller types save space: a single takes 4 bytes against 8 for a double, and uint8 takes 1 (see Numeric Types and Precision).
  • Sparse matrices (sparse) store only nonzero values and are essential when most entries are zero.

Using more cores

Built-in functions such as matrix multiplication and fft already use several cores automatically. For your own loops, MATLAB's Parallel Computing Toolbox provides parfor, which runs independent iterations on multiple workers:

MATLAB
results = zeros(1, 100);
parfor k = 1:100
    results(k) = slow_simulation(k);   % iterations must not depend on each other
end

This needs the toolbox and has startup overhead, so it pays off only when each iteration is expensive. Octave's parallel package and parcellfun offer something similar. Treat parallelism as the last step, after vectorization and preallocation.

A short checklist

  1. Run the profiler and find the hot spot.
  2. Replace loops with array operations where natural (Vectorization).
  3. Preallocate every array that a loop fills.
  4. Loop down columns, not across rows.
  5. Remove repeated work from loops (compute constants once).
  6. Only then consider single, sparse matrices or parfor.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is out(end+1) = value slow inside a long loop?

    Growing an array repeatedly forces memory allocation and copying. Preallocating with zeros or nan avoids it.
  2. 2. Which loop order is best for a MATLAB matrix A?

    MATLAB stores data column by column, so the inner loop should vary the first index to read neighboring memory.
  3. 3. What is the most reliable first step when code is slow?

    Measuring shows which lines matter. Optimizing without data often changes code that was never the problem.
  4. 4. What does tic followed later by toc return?

    tic starts a stopwatch; toc reports the seconds elapsed, and elapsed = toc stores that number.

Summary

  • Measure first: tic/toc, timeit, and the profiler (profile on, profile viewer).
  • Preallocate arrays before loops; avoid growing with end+1.
  • Storage is column-major, so vary the first index in the innermost loop, or avoid the loop with array operations.
  • Be mindful of copies, large temporaries and data types; use sparse when most entries are zero.
  • parfor and the Parallel Computing Toolbox help only when iterations are independent and expensive.
  • The TryIts here are NumPy stand-ins that print comparisons, not machine-specific times.

Next, turn numbers into pictures with 2-D Plots.