Arrays vs tuples / lists
Tue May 18 13:05:35 CEST 2004
thanks for doing the benchmarking for me.
One note: the ets lookup is done with
[X] = ets:lookup(Tab, Pos),
which involves a pattern matching on the lhs, that has a cost.
Could you re-run it on the same box, with
X = head(ets:lookup(Tab, Pos)),
or some such? Head must do a similar check, but presumably it is cheaper.
> Ok, that was perhaps a bit too terse, but I was sitting with
> an impatient baby in my lap (she hasn't quite grasped the
> beauty of Erlang yet, but I'm working on it.)
> test:tup(Iterations, DataSize) runs a tight loop accessing
> objects via element/2 in a tuple with 10,000 elements.
> test:ets/2 does the same thing, but with an ets set containing
> 10,000 objects.
> Due to the very limited precision of erlang:now() on my
> windows box, I ran 1,000 iterations where I accessed all
> 10,000 objects each iterations.
> As you can see, tuple accesses are independent of data size,
> as the data already resides on the process heap. With ets,
> there is always copying, so as the object size increases, so
> does the cost of a lookup.
> Even with the smallest type of payload (an atom), element/2
> is nearly an order of magnitude faster than ets:lookup/2.
> Note, this is only read access. As soon as you start using
> setelement/3 on a tuple of size 10,000, the picture shifts
> rather dramatically.
> On Mon, 17 May 2004 21:14:11 +0200, Ulf Wiger <> wrote:
>> On Mon, 17 May 2004 18:13:01 +0200 (CEST), Thomas Johnsson
>> <> wrote:
>>>> As long as you only look up stuff in large tuples,
>>>> access is very fast and O(1).
>>> Splendid! Presumably it is faster than an ets table lookup with an
>>> as key; by how much, roughly?
>>> -- Thomas
>> Well, it's reasonably easy to measure (at least on UNIX...)
>> (I've enclosed a small test program, and a test run on a 3GHz XP box.)
>> Erlang (BEAM) emulator version 5.3 [threads:0]
>> Eshell V5.3 (abort with ^G)
>> 1> cd("c:/cygwin/home/foo/erl").
>> 2> c(test).
>> 3> test:tup(1000,small).
>> 4> test:tup(1000,medium).
>> 5> test:tup(1000,big).
>> 6> test:ets(1000,small).
>> 7> test:ets(1000,medium).
>> 8> test:ets(1000,big).
>> 9> test:empty(1000).
> Ulf Wiger
More information about the erlang-questions