My test might not be strict. I made several tests again, and found that the performance of matfile can be affected by the column or row loading. But it's more complicate than what I thougt.
If we store matrix data as matrix in .MAT file, I feel like 'matfile' will only show fast speed when loading the shorter dimension. For example, matrix with size of MxN, if M<N, matfile will be faster if it loads columns like this 'tmp = matrixMatObj(:,k)'; otherwise, matfile will be faster if it load rows.
Besides, if we store matrix data as cell array in .MAT file, I feel like 'matfile' will always be as least two times fatser no matter the loading dimension.
Is there anybody who can tell me why matfile works like this?
% test1: compare matrix/cell storage
% ----- load columns
% Two take aways
% 1. if n1<n2,
% with matrix storage, matfile is tens times faster than load
% with cell array storage, matfile is 2 times faster than load
% 2. if n1>n2
% with matrix storage, matfile is almost same as load
% with cell array storage, matfile is still 2 times faster than load
%
clear
n1 = 4000;%4000
n2 = 5000;%3000
ntimes = 20;
uxt = rand(n1,n2);
mattestFile = 'test1.mat';
save(mattestFile,'uxt','-v7.3');
% ---------- matrix store and matfile PART load
tic
uxtMatrix = zeros(n1,ntimes);
for i = 1:ntimes
deconMat = matfile(mattestFile);
tmp = deconMat.uxt(:,20);
uxtMatrix(:,i) = tmp;
end
toc
% ---------- matrix store and common ALL load
tic
uxtMatrix2 = zeros(n1,ntimes);
for i = 1:ntimes
load(mattestFile,'uxt');
uxtMatrix2(:,i) = uxt(:,20);
end
toc
isequal(uxtMatrix, uxtMatrix2)
% ---------- cell store and matfile PART load
uxt = mat2cell(uxt', ones(n2, 1));
mattestFile = 'test2.mat';
save(mattestFile,'uxt','-v7.3');
tic
uxtMatrix = zeros(n1,ntimes);
for i = 1:ntimes
deconMat = matfile(mattestFile);
tmp = deconMat.uxt(20,1);
uxtMatrix(:,i) = tmp{1};
end
toc
% ---------- cell store and common ALL load
tic
uxtMatrix2 = zeros(n1,ntimes);
for i = 1:ntimes
load(mattestFile,'uxt');
uxtMatrix2(:,i) = uxt{20};
end
toc
%
isequal(uxtMatrix, uxtMatrix2)
%% test2: compare matrix/cell storage
% ----- load rows
% Two take aways
% 1. if n1>n2,
% with matrix storage, matfile is tens times faster than load
% with cell array storage, matfile is 2 times faster than load
% 2. if n1<n2
% with matrix storage, matfile is almost same as load
% with cell array storage, matfile is still 2 times faster than load
%
%
clear;
n1 = 5000;
n2 = 4000;
ntimes = 20;
uxt = rand(n1,n2);
mattestFile = 'test3.mat';
save(mattestFile,'uxt','-v7.3');
%
tic
uxtMatrix = zeros(n2,ntimes);
for i = 1:ntimes
deconMat = matfile(mattestFile);
uxtMatrix(:,i) = deconMat.uxt(20,:);
end
toc
%
tic
uxtMatrix2 = zeros(n2,ntimes);
for i = 1:ntimes
load(mattestFile,'uxt');
uxtMatrix2(:,i) = uxt(20,:);
end
toc
%
isequal(uxtMatrix, uxtMatrix2)
%
uxt = mat2cell(uxt, ones(n1, 1));
mattestFile = 'test4.mat';
save(mattestFile,'uxt','-v7.3');
tic
uxtMatrix = zeros(n2,ntimes);
for i = 1:ntimes
deconMat = matfile(mattestFile);
tmp = deconMat.uxt(20,1);
uxtMatrix(:,i) = tmp{1};
end
toc
%
tic
uxtMatrix2 = zeros(n2,ntimes);
for i = 1:ntimes
load(mattestFile,'uxt');
uxtMatrix2(:,i) = uxt{20};
end
toc
%
isequal(uxtMatrix, uxtMatrix2)