% Example of locality in memory allocation
clear all
close all
m = 2000; % Try 1000, 2000, 5000
n = 10000;
% A smart way to do it
tic
a = zeros(m,n);
toc
clear a
% A not so smart way to do it
% a = zeros(m,n); % Comment in to check latency even with preallocation
tic
for j = 1:n
for i = 1:m
a(i,j) = 0.d0;
end
end
toc
clear a
% A really dumb way to do it
% a = zeros(m,n); % Comment in to check latency even with preallocation
tic
for i = 1:m
for j = 1:n
a(i,j) = 0.d0;
end
end
toc