1

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

2
  • What is the speed if you don't compile this? Have you looked at the generated C code? That would tell us more than the original Matlab. Also, these are a simple enough examples that you could write them yourself in C. Commented Dec 11, 2014 at 15:36
  • @horchler Bit tricky to test, because input is 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. Commented Dec 11, 2014 at 17:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.