stevensonmt

stevensonmt

Elixir Map vs Erlang :array

It appears that the Elixir Map implementation is mostly an ergonomic wrapper for :maps in Erlang. In many graph/network exercises I’ve been faced with the need to convert a structure of List of Lists to something accessible by index. I have been mostly doing this by

list 
|> Enum.map(fn row -> row |> Enum.with_index() |> Enum.map(fn {n, i} -> {i, n} end) |> Map.into() end)
|> Enum.with_index() 
|> Enum.map(fn {v, k} -> {k, v} end) 
|> Map.new()

So that I end up with %{0 => %{0 => n_0 ... n => n_n } ... } that I can access with graph[row][col].
Would Erlang arrays be a more efficient structure for repeated access? I’m assuming more than 32 elements, so the large map vs small map distinction is not the determining factor. In my mind Maps were lists of two-tuples but I’m not sure that’s right, at least for large maps. Arrays seem to be implemented as tuples of n-tuples. My instinct is that arrays would be more efficient at write functions but maps more efficient at read/access functions. Am I on the right track? Any tips on setting up a benchmark?

Most Liked

hauleth

hauleth

Not only library, but also the benchmarks:

https://github.com/Qqwy/elixir-arrays

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

You should also try %{{0, 0} => value} that is accessed graph[{row, col}]. I have a feeling this is faster since it’s one lookup not two.

al2o3cr

al2o3cr

One minor code-golf: anytime you see Enum.map |> Map.new/1 you can use Map.new/2 instead.

I doubt there’s a material performance difference (it’s still :maps.from_list/1 ultimately building the output) but it’s definitely less characters :stuck_out_tongue:

Last Post!

stevensonmt

stevensonmt

@hauleth Brilliant, thanks. Looking at the graphs in the benchmark section of the library’s readme I’m not sure I understand the conclusions in the corresponding text about the ErlangArray vs MapArray implementations. For instance:


To me the different implementations seem identical at sizes >8k according to the graph. In any case they seem to be close enough to equivalent for my purposes. I’m glad someone has already put this work in.

@benwilson512 Thanks for the tip. You are right of course about the efficiency, but the tradeoff would be n lookups for n length rows if I ever wanted to deal with things by row. It also is just easier for me to keep it visualized in my head.

@al2o3cr That’s a good tip. Thanks.

Where Next?

Popular in Discussions Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54996 245
New
PragTob
Hey everyone, this has been brewing in my head some time and it came up again while reading Adopting Elixir. GenServers, supervisors et...
New
wmnnd
The Go vs Elixir thread got me thinking: Would it be too hard to implement a simple mechanism for creating Go-style static app binaries f...
New
chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
slashdotdash
Phoenix Live View is now publicly available on GitHub. Here’s Chris McCord’s tweet announcing making it public.
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
paulanthonywilson
I like Umbrella projects and pretty much always use them for personal Elixir stuff, especially Nerves things. But I don’t think this is ...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement