[erlang-questions] Why are arrays faster than tuples?

Jachym Holecek freza@REDACTED
Thu Mar 26 22:30:26 CET 2009

# ryeguy 2009-03-26:
> I was looking at the benchmark at
> http://thinkerlang.com/2008/08/25/optimizing-erlang-a-death-match-of-arrays-and-tuples.html
> and it looks like arrays are many many times faster than tuples. Why
> is this?

You mean "faster WRT modification time", right? Because tuples
are a clear winner for access time in that benchmark.

> I understand that arrays are preallocated while tuples must
> be resized, but I thought that in erlang all arguments are copied when
> they are passed to a function?

No, they're passed by reference and only copied when crossing
process boundary. That's fine because all values are immutable
at language level.

> Am I mistaken or are there just some
> exceptions as to what types of terms are [not] copied?

The explanation is probably that setelement/3 is slow for
large tuples because the whole tuple needs to be copied
when doing so (the exact value of "large" would depend
on CPU type and cache size and kind of memory chips used,
I guess).

The other containers tested need to modify lesser amount
of much smaller terms compared to plain setelement/3 on
a huge tuple.

I didn't look closely, really, but I'm sure someone will
correct me if I'm wrong. ;-)

	-- Jachym

More information about the erlang-questions mailing list