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 Responses

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

vans163
So useless benchmarks aside, Its possible to write a webserver that can serve 300k requests per second (perhaps more with optimizations)....
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
Fl4m3Ph03n1x
Background This question comes mainly from my ignorance. Today is Black Friday, one of my favorite days of the year to buy books. One boo...
New
sashaafm
Piggy backing a bit on @dvcrn topic BEAM optimization for functions with static return type?, I’ve been trying to understand in a deeper ...
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
lucaong
Hello Elixir and Nerves community, I have been working for a while on an open-source embedded key-value database for Elixir, that I call...
230 13924 124
New
nunobernardes99
Hi there Elixir friends :vulcan_salute: In a recent task I was on, I needed to check in two dates which of them is the maximum and which...
New
mbenatti
Following https://github.com/tbrand/which_is_the_fastest |> https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
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
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

Other popular topics Top

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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement