Why does writing to an unrelated file cause the load function to be so slow?

懵懂的女人 提交于 2019-12-05 14:39:54

I do not see any evidence of a bug in your post.

You mention that the entire loop takes about 4.111 and the profiler shows that line 26 takes about 4.11.

This means that all other lines together take less than 0.01 and therefore each line takes a rounded amount of zero seconds.

My guess is that zeroes are just not printed and that you interpreted this as the other lines not being timed.

I may be missing something but so far the output provided by MATLAB seems to be consistent.

Actually, I think I've solved it. I was wrong to jump to the conclusion that the additional processing time was occurring on the new lines, so my question is now a little misleading - the profiler is correct. However, I still didn't understand why writing to a temporary file would cause load to slow down. I had a thought, which was to try this:

file = fopen(sprintf('../temp_logfile_%d', i), 'w');

That is, write to a file in the parent directory instead of the current working directory. This removed the problem, and was very fast. The reason, I am guessing, is that the current directory is in my MATLAB search path, as are a bunch of other directories. I presume that every time MATLAB uses a function which looks though the whole search path, as load does, it checks to see if any directories have been modified, and if so re-parses the whole lot to see what files are available. Writing a new file to the working directory certainly would have caused this. This may have been worse in my case since I also have a whole tree of subdirectories in the working directory which are part of the search path.

Anyway, thanks to those who had a look and sorry that the answer turned out to be something quite different from the question. Be aware when using functions which rely on the entire search path!

I get the following report generated by the profiler of MATLAB 2012b, I dont see a bug.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!