I have the following code to read from standard in, in my compiled Matlab code, which I compiled using mcc -m -T link:exe -N -v -R -nojvm -d build test1.m -o test1
function test1()
% First, read input stream
tic;
stdin=char(0);
while ~strcmp(stdin,'EOF')
%while ~isempty(stdin)
stdin=input(char(0),'s');
end
toc;
% Now, read file
tic;
fid=fopen('test1read.txt');
tline=fgetl(fid);
while ischar(tline)
tline=fgetl(fid);
strcmp(tline,'EOF');
end
fclose(fid);
toc;
end
I executed it as type test1stream.txt | test1.exe (on Windows)
For an order 1.5MB file, I get timing output as
Elapsed time is 1.000616 seconds.
Elapsed time is 0.156772 seconds.
I was a bit surpised to see that reading from input is slower than reading from a file.
For an order 1.5GB file (longer and more lines), I get a timing as
Elapsed time is 78.877386 seconds.
Elapsed time is 95.457972 seconds
My first question would be: why is that? I think one reason is that input() is doing more things than necessary. Is this assumption correct?
My second question is: can I speed up the read from input in a stand-alone tool?
See also: Using standard io stream:stdin and stdout in a matlab exe
inputis waiting for input, right? So user delay will be there. I am not sure what you mean by generated C code? I use the Matlab Compiler, but not in library mode nor do I use mex-files.