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:

Where Next?

Popular in Discussions Top

Jayshua
I recently came across the javascript library htmx. It reminded me a lot of liveview so I thought the community here might be interested....
New
sashaafm
I’m trying to evaluate the best combo/stack for a BEAM Web app. Right now I’m exploring Yaws a bit, after having dealt with Phoenix for a...
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
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
chulkilee
Here are the list of HTTP client libraries/wrappers, and some thoughts on HTTP client in general. I’d like to hear from others how they w...
New
AstonJ
Can you believe the first professionally published Elixir book was published just 8 years ago? Since then I think we’ve seen more books f...
New
joeerl
I’m playing with Elixir - It’s fun. I think @rvirding does give Elixir courses these days. Re: files and database - when I given Erlang ...
New
scouten
I’m looking for a host for the server part of a small (personal) side project that I’m working on. It’s currently written in Node.js and ...
New
griffinbyatt
Sobelow Sobelow is a security-focused static analysis tool for the Phoenix framework. For security researchers, it is a useful tool for g...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

We're in Beta

About us Mission Statement