optimization ?

Chris Pressey cpressey@REDACTED
Tue Apr 22 08:09:11 CEST 2003


Hello,

On Fri, 18 Apr 2003 21:11:09 -0400 (EDT)
HP Wei <hp@REDACTED> wrote:

> Hi, I (a newbie in Erlang)
> am trying to get used to Erlang's way of doing things.
> I have a small module in the following 
> whose function is to extract a list
> of subdirectories under the Top directory.
> It works.  But it is a bit slow 
> (5 sec for a specific directory on our machine).
> Is this the best I can do ??
> [...]
> dirs(Top, [F|Tail], Acc) ->     
>     F2 = Top ++ "/" ++ F,
>     case file:read_link_info(F2) of    
> 	{ok, FileInfo} when FileInfo#file_info.type == directory ->    
> 	    case dirs(F2) of    
> 		{error, Reason} ->
> 		    {error, Reason};
> 		List -> 
> 		    T = [F2|List],
> 		    dirs(Top, Tail, T ++ Acc)
                                    ^^^^^^^^
This is probably one reason that it's kind of slow.  To perform T ++
Acc, the runtime has to search for the end of the T list, which is
expensive compared to a simple cons.  You might want to try [T|Acc]...

> 	    end;	
> 	Other -> 
> 	    dirs(Top, Tail, Acc)
>     end;
> 
> dirs(_Top, [], Ack) -> Ack.

...then lists:flatten/1 the result here before you return it, if you
wish.

Hope that helps,
-Chris



More information about the erlang-questions mailing list