我在编译的Matlab代码中使用以下代码从标准中读取,我使用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
我将其作为type test1stream.txt | test1.exe
(在Windows上)执行
对于1.5MB的订单文件,我得到时序输出为
Elapsed time is 1.000616 seconds.
Elapsed time is 0.156772 seconds.
我有点惊讶地发现从输入读取比从文件读取要慢。
对于订单1.5GB文件(更长和更多行),我得到一个时间
Elapsed time is 78.877386 seconds.
Elapsed time is 95.457972 seconds
我的第一个问题是:为什么?我认为一个原因是input()
做的事情多于必要。这个假设是否正确?
我的第二个问题是:我可以在独立工具中加快读取输入吗?
另请参阅:Using standard io stream:stdin and stdout in a matlab exe