In the following code, why does Matlab alternate between fast and slow reads?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



In the following code, why does Matlab alternate between fast and slow reads?



I am experiencing some strange behaviour in an code that reads a matrix from the hard disk.



I first write a binary file to the hard disk containing a matrix a:


a


N=17000;
a=rand(N);
fid=fopen('a','Wb');
fwrite(fid, a, 'double');
fclose(fid);



Now, I try reading the opening, reading, and closing the file to see how long the whole procedure takes. Each iteration should take the same amount of time.


for ii=1:10
tic
fid = fopen('a', 'r');
matrix=fread(fid, [N, N], 'double');
fclose(fid);
toc
end



Interestingly, the time it takes to read the file alternates between fast and slow! Is there any explanation for this? Here are the timings for the above loop:


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.





I cannot recreate this behaviour on my MacBook pro, with version R2018a, I get a consistent read time of 1.7 seconds. I can neither recreate it on a Ubuntu 16.04.5 platform with version 2017a. Can you please update the question with relevant information about your system. I guess the problem is simply your hdd
– Nicky Mattsson
Aug 13 at 12:00






Thanks for the suggestion. I am reading from the scratch space on a cluster that as a Parallel high-performance filesystem (GPFS). I can reproduce it on my MacBook Pro with SSD to a lesser extent. (only the second read takes longer). Elapsed time is 1.946882 seconds. Elapsed time is 2.757356 seconds. Elapsed time is 1.415333 seconds. Elapsed time is 1.312673 seconds. Elapsed time is 1.314728 seconds. Elapsed time is 1.355692 seconds. Elapsed time is 1.328922 seconds. Elapsed time is 1.331262 seconds. Elapsed time is 1.322359 seconds. Elapsed time is 1.421541 seconds.
– avgn
Aug 15 at 2:21




1 Answer
1



That's very interesting, I wonder if it has to do with the preceding of opening it closing a file. A way to test that possibility may be to add delay(2) in the loop and then see if the times match.





Good guess! But adding pause(2) after the toc did not change the timings.
– avgn
Aug 12 at 23:20






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard