optimization of list comprehensions

Ulf Wiger (AL/EAB) <>
Fri Mar 10 10:20:17 CET 2006


 

Here's another way to find the info:

1> Loaded = erlang:loaded().
2> Imps = lists:map(fun(M) -> case
beam_lib:chunks(code:which(M),[imports]) of {ok, {_,[{imports,Is}]}} ->
{M,Is}; _ -> {M,undefined} end end, Loaded).
3> GoodImps = [X || {_,I} = X <- Imps, I =/= undefined].
4> {length(GoodImps), NLoaded = length(Loaded)}.
5> UsersOf = fun(MFA) -> L = length([M || {M,I} <- GoodImps,
lists:member(MFA,I)]), {L, round(100*L/NLoaded)} end.
6> [{MFA,UsersOf(MFA)} || MFA <-
[{lists,foldl,3},{lists,map,2},{lists,foreach,2}]].

In my test system (an erlang node in a product we're currently working
on):

- Loaded modules: 953 (947 with 'imports' info)
- modules using
  + lists:foldl/3:   145 (15%)
  + lists:map/2:     168 (18%)
  + lists:foreach/2: 156 (16%)
    % more esoteric lists functions
  + lists:merge/3:     0 (0%)
  + lists:sort/2:      7 (1%)
  + lists:umerge/3:    0 (0%)
  + lists:usort/2:     1 (0%)
  + lists:zipwith/3:   2 (0%)
  + lists:zipwith3/4:  0 (0%)
    % other "advanced functions"
  + ets:select/2:       20 (2%)
  + ets:select/1:        5 (1%)
  + ets:select_delete/1: 3 (0%)

Formatting got tiresome. I'll just post the raw data:

[{{mnesia,dirty_read,1},{58,6}},
 {{mnesia,dirty_read,2},{34,4}},
 {{mnesia,dirty_write,1},{73,8}},
 {{mnesia,dirty_write,2},{3,0}}]

[{{mnesia,activity,2},{8,1}},
 {{mnesia,activity,3},{1,0}},
 {{mnesia,activity,4},{0,0}},
 {{mnesia,transaction,1},{33,3}},
 {{mnesia,transaction,2},{2,0}}]

Moving on to funs:

7> Locals = lists:map(fun(M) -> case
beam_lib:chunks(code:which(M),[locals]) of {ok, {_,[{locals,Ls}]}} ->
{M,Ls}; _ -> {M,undefined} end end, Loaded).
8> GoodLocals = [X || {_,I} = X <- Locals, I =/= undefined].
9> IsAFun = fun({F,_A}) -> hd(atom_to_list(F)) == $- end.
10> HasFuns = [{M,Ls} || {M,Ls} <- GoodLocals, lists:any(IsAFun, Ls)].

11> length(HasFuns).
499

12> length(GoodLocals).
947

13> IsAnLc = fun({F,_A}) -> regexp:match(atom_to_list(F),"lc\\$") =/=
nomatch end.
14> HasLcs = [{M,Ls} || {M,Ls} <- GoodLocals, lists:any(IsAnLc, Ls)].

15> length(HasLcs).
251

So, 499 modules out of 947 seem to have funs,
and 251 of those modules seem to have LCs.


Note that among the loaded modules are both OTP modules (from those
applications that we use), and our modules. I've not bothered to
separate them.

/Ulf W

> -----Original Message-----
> From:  
> [mailto:] On Behalf Of Mats Cronqvist
> Sent: den 10 mars 2006 08:53
> To: 
> Subject: Re: optimization of list comprehensions
> 
> 
> 
> Robert Virding wrote:
> > You will probably find an even grater usage if you start searching 
> > more seriously.
> 
>    yes, it's by no means a serious study.
> 
> > I, for one, usually do an import on 'lists' so I don't have 
> to write 
> > the lists: prefix all the time.
> 
>    so do i. but i doubt accounting for -import(lists) would 
> change the numbers significantly.
> 
>    mats
> 



More information about the erlang-questions mailing list