How to optimize write operation to disk for faster execution?

8 views (last 30 days)
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);

Accepted Answer

per isakson
per isakson on 6 Feb 2020
Edited: per isakson on 6 Feb 2020
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!

More Answers (0)

Community Treasure Hunt

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

Start Hunting!