Memory

Passing structure field vs. temporary variable

For a large structure A, it's fine to pass a field as opposed to creating a temporary variable. Once it gets to ~6GB, it's slightly faster to create a temporary variable, but otherwise just pass the field.


Example: 

Create a structure t

s='A':'Z';
for i = 1:26
t.(s(i)) = rand(5000);
end

Time

disp('tmp')
tic
for i = 1:26
tmp = t.(s(i));
dummy = prod(tmp);
end
toc

disp('structure')
tic
for i = 1:26
dummy = prod(t.(s(i)));
end
toc