dlmwrite vs. writematrix: speed

30 visualizzazioni (ultimi 30 giorni)
tetra
tetra il 17 Mar 2023
Commentato: tetra il 18 Mar 2023
dlmwrite, according to its page, is not recommended. It is suggested to use writematrix instead (better performance). I currently use dlmwrite to continuously append a text file with measurement data. Speed is therefore important. Comparing dlmwrite to writematrix,
fwrtmat = 'test writematrix.txt';
fdlmwrt = 'test dlmwrite.txt';
ncols = 10; % number of columns
niter = 1e3; % number of iterations in test
fmt = ['%f\t\n',repmat('%f\t ', 1, ncols)]; % format for dlmwrite
fmt(end:end+1) = '\n';
fid = fopen(fdlmwrt, 'w');
fclose(fid);
tic
for i = 1:niter
data = rand(1,ncols);
writematrix(data,fwrtmat,'WriteMode','append','FileType','text','Delimiter','tab');
end
toc
Elapsed time is 1.890530 seconds.
tic
for i = 1:niter
data = rand(1,ncols);
dlmwrite(fdlmwrt,data,'-append','delimiter','\t','precision',15); % use same precision as writematrix
end
toc
Elapsed time is 0.283388 seconds.
I found that dlmwrite is faster, which I did not expect. Additionally, the (default?) precision of writematrix is not required and it would even be preferential to sacrifice precision for performance. The file format can be changed if that would improve performance.
Am I using writematrix correctly/optimally in my example? Should I use writematrix or stick to dlmwrite?

Risposta accettata

Sulaymon Eshkabilov
Sulaymon Eshkabilov il 17 Mar 2023
If you run this simulation on the MATLAB desktop and shift the orders, their elapsed time will be similar. Anyhow it is interesting :)
Here is another discussion thread demonstrating your conclusion, i.e., dlmwrite() is faster than writematrix(): https://www.mathworks.com/matlabcentral/answers/653363-fastest-way-to-append-file-with-matrix-data
fwrtmat = 'test writematrix.txt';
fdlmwrt = 'test dlmwrite.txt';
ncols = 10; % number of columns
niter = 1e3; % number of iterations in test
fmt = ['%f\t\n',repmat('%f\t ', 1, ncols)]; % format for dlmwrite
fmt(end:end+1) = '\n';
fid = fopen(fdlmwrt, 'w');
fclose(fid);
tic
for i = 1:niter
data = rand(1,ncols);
dlmwrite(fdlmwrt,data,'-append','Delimiter','\t','precision',15); % use same precision as writematrix
end
TDLM=toc
tic
for i = 1:niter
data = rand(1,ncols);
writematrix(data,fwrtmat,'WriteMode','append','FileType','text','Delimiter','\t');
end
TWRTM=toc
  1 Commento
tetra
tetra il 18 Mar 2023
Thank you for recommending that thread. It appears I did not search Matlab answers thoroughly. I explored the suggestion of using a binary file. Using similar code:
fbinwrt = 'test binwrite.bin';
ncols = 10; % number of columns
niter = 1e3; % number of iterations
fidbin = fopen(fbinwrt,'a'); % open binary file, 'a' = append data
tic
for i = 1:niter
data = rand(1,ncols);
fwrite(fidbin,data,'double'); % format double
end
fclose(fidbin); % close before reading
TBIN = toc
TBIN = 0.0063
This is ~100 times as fast as dlmwrite on desktop!
Now to open the file (less straightforward to me than when using dlmwrite and writematrix):
bincheck=fopen(fbinwrt,'r'); % 'r' = read data
bindata=fread(bincheck,[ncols,inf],'double')'; % transposing will result in 'correct' size
fclose(bincheck);
And indeed as you mentioned, the elapsed times between dlmwrite and writematrix were somewhat similar on my Matlab desktop. dlmwrite was about twice as fast as writematrix.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Data Import and Export in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by