optimization ?

HP Wei hp@REDACTED
Sat Apr 19 03:11:09 CEST 2003


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

[In comparision, my old python code generates the list
 in about 1.8 sec. But that code uses a global variable as
 the accumulator (Acc), and one recursive function. 
 And that scheme cannot be duplicated in Erlang because of 
 the immutable Variable.]
 
thanks,
HP

% -----------------test.erl --------------------------
-module(test).
-include_lib("kernel/include/file.hrl").
-export([dirs/1]).

dirs(Top) ->
    case file:list_dir(Top) of
	{ok, Entries} ->        
	    dirs(Top, Entries, []);   
	{error, Reason} ->
	    {error, {Top, Reason}}
    end.

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)
	    end;	
	Other -> 
	    dirs(Top, Tail, Acc)
    end;

dirs(_Top, [], Ack) -> Ack.
% -----------------------------------------------




More information about the erlang-questions mailing list