# [erlang-questions] Performance of maps:get()

Avinash Dhumane nistrigunya@REDACTED
Wed May 18 05:35:49 CEST 2016

```For my algorithm trading application, gains even in 5 to 10 microseconds
matter.

I used maps extensively to improve expressiveness of the design.

But, here is a small observation about maps. I am not sure if the
performance variation seen is indeed true, or if my test case is wrong. In
either case, please point me to the right way of efficiently using of maps.

avinash@REDACTED:~/tws\$ erl
Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:2:2] [async-threads:10]
[hipe] [kernel-poll:false]

Eshell V7.3  (abort with ^G)

%%% compute average of timer:tc() return values (timings)

1> Avg = fun(PerfList) -> Sum = lists:sum(lists:map(fun({Time, _}) -> Time
end, PerfList)), Len = erlang:length(PerfList), Sum div Len  end.
#Fun<erl_eval.6.50752066>

%%% a test map of size 3

2> Map = #{{a, 1} => 100, {a, 2} => 200, {a, 3} => 300}.
#{{a,1} => 100,{a,2} => 200,{a,3} => 300}

%%% performance: without maps:get()

3> PerfList1 = lists:map(fun(_) -> timer:tc(fun() -> K1 = {a, 1}, K2 = {a,
2}, K3 = {a, 3}, #{K1 := V1, K2 := V2, K3 := V3} = Map, {V1, V2, V3} end)
end, lists:seq(1,100)).
[{111,{100,200,300}},
{81,{100,200,300}},
{88,{100,200,300}},
.....details deleted.....
{91,{100,200,...}},
{83,{100,...}},
{85,{...}},
{107,...},
{...}|...]

%%% performance: with maps:get()

4> PerfList2 = lists:map(fun(_) -> timer:tc(fun() -> {maps:get({a,1}, Map),
maps:get({a,2}, Map), maps:get({a,3}, Map)} end) end, lists:seq(1, 100)).
[{13,{100,200,300}},
{12,{100,200,300}},
{33,{100,200,300}},
.....details deleted.....
{13,{100,200,...}},
{12,{100,...}},
{12,{...}},
{12,...},
{...}|...]

%%% compare performance: maps:get() is 7-times faster

5> {Avg(PerfList1), Avg(PerfList2)}.
{89,13}
6>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160518/af0e4b67/attachment.htm>
```