dlmwrite vs. writematrix: speed
27 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
tetra
am 17 Mär. 2023
Kommentiert: tetra
am 18 Mär. 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
tic
for i = 1:niter
data = rand(1,ncols);
dlmwrite(fdlmwrt,data,'-append','delimiter','\t','precision',15); % use same precision as writematrix
end
toc
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?
0 Kommentare
Akzeptierte Antwort
Sulaymon Eshkabilov
am 17 Mär. 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 Kommentar
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Text Files finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!