[erlang-questions] Indexed element access: array vs. erlang:element

Richard Carlsson <>
Mon Aug 25 22:04:20 CEST 2008


Dimitry Golubovsky wrote:
> I need an index-based access to elements of a fixed-size array-like
> structure. Number of elements is expected to be about several
> thousands.
> 
> What would be more efficient: to create an array (using the "array"
> module) or to build a huge tuple and use erlang:element?

If it's the case of write-once, read-many, you could prepare the
data in a list, do list_to_tuple, and then use the tuple for reading.
But I'd only do that myself as a last resort, to maximize speed.

> PS I suspect the latter, but how does erlang:element scale on tuples
> of such size?

Just reading elements using erlang:element(N, Tuple) is as fast as
it can possibly get - simply a pointer addition and a load. And tuples
can be pretty huge. Try e.g. erlang:make_tuple(10000000,0)) - that's
10 million elements. But updating big tuples (copying N words) is much
too inefficient; hence the array module, which tries to provide a good
balance between read and write access times.

    /Richard



More information about the erlang-questions mailing list