[erlang-questions] which one is more efficiency? ets or pattern match?

Fred Hebert mononcqc@REDACTED
Wed Mar 16 14:53:46 CET 2016


On 03/16, Joe Armstrong wrote:
>If the array module is not the most efficient and it turned out that
>(say ets) is more
>efficient then parts of the array module should be rewritten using ets
>to reflect this.

Oh oh oh, this is a dangerous thing! You would be switching the purely 
functional semantics of the array module into destructive ones using ETS 
tables! This is what sofs do for sets, and the usage patterns may change 
entirely!

Moreover, passing the data type over to different processes could stop 
working or have unintended consequences.

The array module should *not* be rewritten if ETS is faster, simply 
because they would behave entirely differently!

>
>If the array indices are in gappy integer sequences then you should use
>sparce_arrays.erl - but this hasn't been written, so go write it :-)
>

It has been written in that the current `array' module does support 
sparse arrays, and can also deal with fixed or resizable arrays:

http://erlang.org/doc/man/array.html

You will find functions for:
- initializing sparse arrays
- doing sparse folds
- doing sparse maps
- converting sparse arrays to lists
- converting sparse arrays to orddicts

The internal representation uses branching trees of tuples and you can 
test for yourself that the representation may be sparse:

11> array:from_list(lists:zip(lists:seq(5,10)++lists:seq(995,1000), lists:seq(1,12))).
{array,12,100,undefined,
       {{{5,1}, {6,2}, {7,3}, {8,4}, {9,5}, {10,6},
         {995,7}, {996,8}, {997,9}, {998,10}},
        {{999,11}, {1000,12},
         undefined,undefined,undefined,undefined,undefined,undefined,
         undefined,undefined},
        10,10,10,10,10,10,10,10,10}}

There is no need for a second array module, since the current one 
handles both sparse and non-sparse arrays fine.

Regards,
Fred.



More information about the erlang-questions mailing list