How to optimize write operation to disk for faster execution?

조회 수: 7 (최근 30일)
Jorge
Jorge 2020년 2월 5일
댓글: Jorge 2020년 2월 7일
I made a script for converting a binary file (e.g. a PDF file) into an ASCII equivalent (in hexadecimal representation), and then performing the inverse operation (i.e. reconstruct the binary file from the ASCII file). It works fine except that the inverse operation takes considerable time to write to the disk (several minutes) for input files of some MB.
How could the code be optimzed for reducing significantly the time spent writing to disk the reconstructed binary file ("Step 2" in the code below)?
Thanks in advance for any help!
P.S.1: If you know a better way to do this altogether, I'd be happy to switch to any other solution!
P.S.2: The code below is just an example: in the intended application of the script the conversion and reconstruction sections are to be executed separately on different machines, using the ASCII data as the transfer medium.
%% Choose input file (binary)
[filename,path] = uigetfile('*.*');
if isequal(filename,0)
disp('User selected Cancel');
return
end
filename_root = filename(1:strfind(filename,'.')-1);
filename_extension = filename(strfind(filename,'.')+1:length(filename));
%% Step 1: Create ASCII file from binary data
% Read binary data
h_file_in = fopen(fullfile(path,filename),'r');
data_bin_in = fread(h_file_in);
data_hex_out = dec2hex(data_bin_in);
fclose(h_file_in);
% Write ASCII data
h_file_out = fopen(fullfile(path,[filename_root '_dumped.txt']),'w');
for i=1:length(data_hex_out)
fprintf(h_file_out,'%s',data_hex_out(i,:));
end
fclose(h_file_out);
%% Step 2: Reconstruct binary file from ASCII data
% Read ASCII data
h_file_in = fopen(fullfile(path,[filename_root '_dumped.txt']),'r');
data_hex_in = textscan(h_file_in,'%s');
data_hex_in = cell2mat(data_hex_in{1});
fclose(h_file_in);
% Write binary data
h_file_out = fopen(fullfile(path,[filename_root '_reconstructed.' filename_extension]),'w');
for i=1:2:length(data_hex_in)
fwrite(h_file_out,hex2dec(data_hex_in(i:i+1)),'uint8');
end
fclose(h_file_out);

채택된 답변

per isakson
per isakson 2020년 2월 6일
편집: per isakson 2020년 2월 6일
Replace
for i=1:length(data_hex_out)
fprintf(h_file_out,'%s',data_hex_out(i,:));
end
by
fprintf( h_file_out, '%s', permute( data_hex_out, [2,1] ) );
and
for i=1:2:length(data_hex_in)
fwrite(h_file_out,hex2dec(data_hex_in(i:i+1)),'uint8');
end
by
fwrite( h_file_out, hex2dec(permute(reshape(data_hex_in,2,[]),[2,1])), 'uint8' );
Make sure it works correctly!

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by