Bin Counting elements in a collection by updating a counter map is much slower than first grouping the elements by bin and then count each bin.
I would be curious to learn why. My intuition was the opposite, as the ‘reduce’ approach requires only one pass through the input collection.
Example:
iex> range = 1..1_000_000
iex> is_even? = fn x -> rem(x,2) == 0 end
iex> Enum.reduce(range, %{}, fn elem, acc -> Map.update(acc, is_even?.(elem), 1, &(&1 + 1)) end)
%{false: 500000, true: 500000}
is much slower than
iex> Enum.group_by(range, &is_even?.(&1)) |> Map.new(fn {key, list} -> {key, length(list)} end)
%{false: 500000, true: 500000}






















