Very slow dir ()

When there are many files, about 4000, the function dir()is very slow. I assume that it creates the structure and populates the values ​​in an inefficient way.

Are there quick and elegant alternatives to use dir()?

Update: testing in 64 bit, Windows 7 using MATLAB R2011a.

Update 2: It takes about 2 seconds to complete.

+3
source share
4 answers

What processor / OS are you using? I just tried this on my machine with a directory with 5000 files, and it is pretty fast:

>> 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

Another alternative to the MATLAB ls and dir functions is to directly use Java java.io.Filein MATLAB:

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

ans =

        5000
+8
source

S , 363 . Win7 64-bit 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, . ...

+6

LS. . , DIR.

UPDATE:

4000 . dir ls : 0,34 . , . (MATLAB 2011a, Windows 7 64-)

? , ?

+1

% :

Folder = 'C:\'; %can be a relative path
jFile = java.io.File(Folder); %java file object
Names_Only = cellstr(char(jFile.list)) %cellstr
Full_Paths = arrayfun(@char,jFile.listFiles,'un',0) %cellstr

% : ( )

Folder = 'C:\';
jFile = java.io.File(Folder); %java file object
jPaths = jFile.listFiles; %java.io.File objects
jNames = jFile.list; %java.lang.String objects
isFolder = arrayfun(@isDirectory,jPaths); %boolean
File_Names_Only = cellstr(char(jNames(~isFolder))) %cellstr

% :

Folder = 'C:\';
jFile = java.io.File(Folder); %java file object
jNames = jFile.list; %java string objects
Match = arrayfun(@(f)f.startsWith('page')&f.endsWith('.sys'),jNames); %boolean
cellstr(char(jNames(Match))) %cellstr

% :

methods(handle(jPaths(1)))
methods(handle(jNames(1)))
+1

All Articles