很慢的目录()

nim*_*cap 3 matlab file dir

当有很多文件,大约4000,dir()功能很慢.我的猜测是它创建了一个结构并以低效的方式填充值.

有没有快速和优雅的替代品使用dir()

更新:使用MATLAB R2011a在64位,Windows 7中进行测试.

更新2:完成大约需要2秒钟.

Jas*_*n S 8

您使用的是哪个CPU/OS?我只是在我的机器上尝试了一个包含5000个文件的目录,它非常快:

>> d=dir;
>> tic; d=dir; toc;
Elapsed time is 0.062197 seconds.
>> tic; d=ls; toc;
Elapsed time is 0.139762 seconds.
>> tic; d=dir; toc;
Elapsed time is 0.058590 seconds.
>> tic; d=ls; toc;
Elapsed time is 0.063663 seconds.
>> length(d)

ans =

        5002
Run Code Online (Sandbox Code Playgroud)

MATLAB的ls和dir函数的另一种替代方法是直接java.io.File在MATLAB中使用Java :

>> f0=java.io.File('.');
>> tic; x=f0.listFiles(); toc;
Elapsed time is 0.006441 seconds.
>> length(x)

ans =

        5000
Run Code Online (Sandbox Code Playgroud)


小智 6

确认了Jason S对网络驱动器和包含363个文件的目录的建议.Win7 64位Matlab 2011a.

两者foo及其bar下面产生相同的文件名单元格数组(使用数据的MD5哈希验证),但bar使用Java所花费的时间要少得多.如果我先生成bar然后生成类似的结果foo,那么这不是网络缓存现象.

>> tic; foo=dir('U:\mydir'); foo={foo(3:end).name}; toc
Elapsed time is 20.503934 seconds.
>> tic;bar=cellf(@(f) char(f.toString()), java.io.File('U:\mydir').list())';toc
Elapsed time is 0.833696 seconds.
>> DataHash(foo)
ans =
84c7b70ee60ca162f5bc0a061e731446
>> DataHash(bar)
ans =
84c7b70ee60ca162f5bc0a061e731446

其中cellf = @(fun, arr) cellfun(fun, num2cell(arr), 'uniformoutput',0);DataHash来自http://www.mathworks.com/matlabcentral/fileexchange/31272.我跳过返回的数组的前两个元素,dir因为它们对应于....