您使用的是哪个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因为它们对应于.和...