public
Created

  • Download Gist
arrays_vs_tuples.erl
Erlang
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
-module(arr).
 
-compile([export_all]).
 
data1(N) ->
%% size implies fixed-size array
%% but lets be explicit
array:new([{size, N}, {default, 0}, {fixed, true}]).
 
data2(N) ->
%% extensible array
array:new([{size, N}, {default, -1}, {fixed, false}]).
 
data3(N) ->
erlang:make_tuple(N, 0).
 
data4(_) ->
gb_trees:empty().
 
array_set(Array, I, Value) ->
%% array indexing starts at 0
array:set(I - 1, Value, Array).
 
tuple_set(Tuple, I, Value) ->
%% tuple indexing starts at 1
setelement(I, Tuple, Value).
 
tree_set(Tree, I, Value) ->
gb_trees:enter(I, Value, Tree).
 
array_get(Array, I) ->
array:get(I - 1, Array).
 
tuple_get(Tuple, I) ->
element(I, Tuple).
 
tree_get(Tree, I) ->
gb_trees:get(I, Tree).
 
get(_, _, 0) ->
ok;
 
get(Fun, Data, N) ->
Fun(Data, N),
get(Fun, Data, N - 1).
 
set(_, Data, 0) ->
Data;
 
set(Fun, Data, N) ->
Data1 = Fun(Data, N, N),
set(Fun, Data1, N - 1).
 
test() ->
test(10000).
 
test(N) ->
%% fixed-size array
{S1, D1} = timer:tc(arr, set, [{arr, array_set}, data1(N), N]),
{G1, _} = timer:tc(arr, get, [{arr, array_get}, D1, N]),
%% extensible array
{S2, D2} = timer:tc(arr, set, [{arr, array_set}, data2(N), N]),
{G2, _} = timer:tc(arr, get, [{arr, array_get}, D2, N]),
%% tuple
{S3, D3} = timer:tc(arr, set, [{arr, tuple_set}, data3(N), N]),
{G3, _} = timer:tc(arr, get, [{arr, tuple_get}, D3, N]),
%% gb_trees
{S4, D4} = timer:tc(arr, set, [{arr, tree_set}, data4(N), N]),
{G4, _} = timer:tc(arr, get, [{arr, tree_get}, D4, N]),
%% results
io:format("Fixed-size array: get: ~8wµs, set: ~8wµs~n", [G1 , S1]),
io:format("Extensible array: get: ~8wµs, set: ~8wµs~n", [G2 , S2]),
io:format("Tuple: get: ~8wµs, set: ~8wµs~n", [G3 , S3]),
io:format("Tree: get: ~8wµs, set: ~8wµs~n", [G4 , S4]),
ok.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.