[erlang-questions] Fast directory walker

Frank Muller <>
Sat Dec 10 09:43:14 CET 2016


Hi Michael,

Better, but still ~2 seconds:

> timer:tc(fun() -> filelib:fold_files("/usr/share", ".*", true, fun(F, N)
-> N + 1 end, 0) end).
{1993074,28953}

If I get it correctly the call matches only on files, not on dirs.

/Frank

Le sam. 10 déc. 2016 à 03:12, Michael Truog <> a écrit :

>
>
> On 12/09/2016 03:15 PM, Frank Muller
>
> wrote:
>
>
>
>
>
>
>
>
> Hi
>
>
>
>
>
>
>
> I would like to improve the speed of my directory
>
> walker.
>
>
>
>
>
>
>
> walk(Dir) ->
>
>
>     {ok, Files} = prim_file:list_dir(Dir),
>
>
>     walk(Dir, Files).
>
>
>
>
>
>
>
> walk(Dir, [ Basename | Rest ]) ->
>
>
>     Path = filename:join([ Dir, Basename ]),
>
>
>     case filelib:is_dir(Path) of
>
>
>         true  ->
>
>
>             walk(Path);
>
>
>         false ->
>
>
>             io:format("~s~n", [Path]),
>
>
>             filelib:file_size(Path)
>
>
>     end,
>
>
>     walk(Dir, Rest);
>
>
> walk(_, []) ->
>
>
>     ok.
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> Compared
>
> to almost anything i found on the web, it’s still very slow:
>
>
>
>
> > timer:tc(fun() -> dir:walk("/usr/share")
>
> end).
>
>
>
>
> {4662361,ok}
>
>
>
>
>
>
>
> Have you tried filelib:fold_files/5
>
> (http://erlang.org/doc/man/filelib.html#fold_files-5) ?
>
>
>
>
>
>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20161210/f13639d3/attachment-0001.html>


More information about the erlang-questions mailing list