难道你不能真正检查你的程序并以不同的方式构造它,以便只从文件内容的部分内存表示中工作吗?
例如,如果要将100 000行附加到3000个文件(即使不需要对文件中已有的内容进行任何表示),您可以这样做:
%% Main processing
function [] FullProcess()
%[
for block = 1:100,
% Partial processing
lines = processBlock(block);
% Save step
pushToFiles(block, lines);
end
%]
附:
% Partial processing in memory
function [lines] = processBlock(block)
%[
% Preallocate
lines = cells(1000, 3000);
% Do the processing for current block
...
lines{500, 12} = 'kikou';
...
%]
和:
%% Save partial work
function [] = pushToFiles(block, lines)
%[
fcount = size(lines, 2);
lcount = size(lines, 1);
for fi = 1:fcount,
[fid, msg] = fopen(fprintf('f%i', fi), 'a'); % Open in append mode
if (fid < 0), error(msg); end
for li = 1:lcount,
fprintf(fid, lines{li, fi});
end
fclose(fid);
end
%]
这减少了100 fopen / fclose的事情(尽管有3000个文件,但这远远低于之前的预期)