Partially loading matirx with matfile doesn't work

1 visualizzazione (ultimi 30 giorni)
I was trying to use matfile to partially load a big matrix storing in a v7.3 .MAT file. I had specified the colum Index but I don't think partially loading work for my case. I test the speed and matfile is just equally slow as load. However, when i tried to load along the row direction, matfile does work much and much faster than load.
Here is my question. Does partially loading function of matfile only work for row direction loading? I think it could be reasonable considering the row oriented memory storage strategy of MATLAB. But i really need confirm this. FYI, I also attached my test codes.
% ----- loading along columns
% matfile shows just equally speed as load
uxtMatrix = zeros(5000,50);
tic
for i = 1:50
Mat = matfile(matFilesv7_3{i});
uxtMatrix(i,:) = Mat.matrix(:,100);
end
toc
%
tic
uxtMatrix2 = zeros(5000,50);
for i = 1:50
load(matFilesv7_3{i},'matrix');
uxtMatrix2(i,:) = uxt(:,100);
end
toc
% ----- loading along rows
% matfile is much much faster! it works here
uxtMatrix = zeros(50,10000);
tic
for i = 1:50
Mat = matfile(matFilesv7_3{i});
uxtMatrix(i,:) = Mat.matrix(20,:);
end
toc
%
tic
uxtMatrix2 = zeros(50,10000);
for i = 1:50
load(matFilesv7_3{i},'matrix');
uxtMatrix2(i,:) = uxt(20,:);
end
toc

Risposte (1)

Feng Cheng
Feng Cheng il 15 Dic 2021
Modificato: Feng Cheng il 15 Dic 2021
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)

Categorie

Scopri di più su Workspace Variables and MAT-Files in Help Center e File Exchange

Prodotti


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by