how to hold lists

Thomas Lindgren thomasl_erlang@REDACTED
Fri May 12 10:58:24 CEST 2006



--- Bengt Kleberg <bengt.kleberg@REDACTED> wrote:

> On 2006-05-12 09:56, Yani Dzhurov wrote:
> >...deleted
> 
> > How would it better and faster to hold them:
> > 
> > -as macros
> > 
> > -define(LIST1, [a,b,c,�..]).
> > 
> >  
> > 
> > or have a function which returns it
> > 
> >  
> > 
> > get_list1()-> [a,b,c,�.].
> 
> macros will force a recompile every time you want to
> change a list.
> in that respect a function is better.

Good point.

> macros might be very slightly faster. measure and
> compare to your 
> requirements. then you know if you can take the
> better solution, or must 
> take the very slightly faster one.

On the other hand, with a macro, you are duplicating
the same byte code many times. My guess is the
function call is just about the same speed: you will
have an extra function call, which may or may not mess
things up, but the bytecode is more likely to be in
the data cache. (On the gripping hand, there's also
the potential cost in code and time of saving and
restoring registers around the function call.)

The salomonic approach is of course to start with the
function and inline expand it "to the macro version"
wherever necessary :-)

An interesting simple option is to just use Hipe (ie,
native compilation). As I recall, Hipe builds known
terms at compile time and shares them.

Option 3: If there are many frequently used, known,
constant lists, then an even better approach could be
to rewrite the operations on them and/or change the
data structure. For example:

lists:member(X, [a,b,...,z]).

=>

my_mem(a) -> true;
...
my_mem(z) -> true;
my_mem(_) -> false.

Best,
Thomas


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the erlang-questions mailing list