How to optimize write operation to disk for faster execution?

8 ビュー (過去 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!
  1 件のコメント
Jorge
Jorge 2020 年 2 月 7 日
Works like a charm isakson, than you so much!!!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by