In MATLAB exist( x, 'file' ) takes forever

只谈情不闲聊 提交于 2019-12-12 08:21:53

问题


I am using exist(x, 'file') to check for the existence of a file on my machine. The execution of this command takes FOREVER (over 10 seconds per call!).

My matlabpath is not too long (about 200 entries) and all folders on path are on my local drive (no network).

  1. Why does exist takes forever?
  2. Is there a way to make it run FASTER?

PS,
This call to exist is part of Matlab's execution of loadlibrary. So, if you are calling loadlibrary and you don't know why it takes forever - this question is also for you.


回答1:


Here's one idea. You could put the directory containing those header files up at the front of the MATLAB path, so when exist() goes looking through the path, it finds them quickly and doesn't have to search through the rest of the entries. If it's spending its time stepping through your path, that may help.




回答2:


Wow! That was a tough one. Bottom line: Delete %TEMP% files!

I had a few thousands files lying around in %TEMP%. It appears MATLAB really likes to go over and over the TEMP directory.

After clearing the TEMP folder, exist runs in no time!

(Thanks Andrew for the Process Monitor advice!)




回答3:


  1. exist is a built in Matlab function. It is designed to check existence of other types of objects (such as variables in Matlab) as well as files. Being a built in function, it's not a simple to see how it is coded. At least on Windows, when you call exist('filename','file') it seemingly only makes one API call to the operating system to check the file existence. So either the operating system is taking a long time, or there is some bloat in the exist function making it run slowly. See the solutions from the other posters for ideas on how to make the operating system return its result more quickly

  2. People sometimes complain that running exist('filename','file') in a loop makes the loop very slow, this is due the call taking perhaps milliseconds and looping over a few thousand times. The solution here is to replace

    if exist('filename','file')   
      % your code

with the line

    if java.io.File('filename').exists
      % your code



回答4:


For 372 files Matlab: Elapsed time is 40.207266 seconds. (get a cup of thee) Java: Elapsed time is 0.122165 seconds. (eye blinking)



来源:https://stackoverflow.com/questions/15786286/in-matlab-exist-x-file-takes-forever

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