我在从硬盘读取矩阵的代码中遇到了一些奇怪的行为。
a
:
N=17000;
a=rand(N);
fid=fopen('a','Wb');
fwrite(fid, a, 'double');
fclose(fid);
现在,我试着阅读打开、读取和关闭文件,看看整个过程需要多长时间。每次迭代所用的时间应该相同。
for ii=1:10
tic
fid = fopen('a', 'r');
matrix=fread(fid, [N, N], 'double');
fclose(fid);
toc
end
有趣的是,读取文件的时间在快速和慢速之间交替进行!对此有什么解释吗?以下是上述循环的计时:
Elapsed time is 1.259988 seconds.
Elapsed time is 2.454427 seconds.
Elapsed time is 1.534250 seconds.
Elapsed time is 2.453246 seconds.
Elapsed time is 1.535322 seconds.
Elapsed time is 2.454762 seconds.
Elapsed time is 1.534847 seconds.
Elapsed time is 2.449777 seconds.
Elapsed time is 1.534265 seconds.
Elapsed time is 2.449074 seconds.