defmodule IterReduce do
def iter_reduce(map, initial_acc, fun) do
do_iter_reduce(:maps.iterator(map), initial_acc, fun)
end
def do_iter_reduce(iter, acc, fun) do
case :maps.next(iter) do
:none -> acc
{k, v, next_iter} -> do_iter_reduce(next_iter, fun.({k, v}, acc), fun)
end
end
end
inputs = %{
"Small map" => Enum.map(1..10, fn el -> {to_string(el), el} end) |> Enum.into(%{}),
"Medium map" => Enum.map(1..1000, fn el -> {to_string(el), el} end) |> Enum.into(%{}),
"Large map" => Enum.map(1..1000000, fn el -> {to_string(el), el} end) |> Enum.into(%{})
}
Benchee.run(%{
"Elixir shit" => fn map -> Enum.reduce(map, {"", 0}, fn {k, v}, {acc_k, acc_v} -> {acc_k <> k, acc_v + v} end) end,
"Iterators" => fn map-> IterReduce.iter_reduce(map, {"", 0}, fn {k, v}, {acc_k, acc_v} -> {acc_k <> k, acc_v + v} end) end,
}, inputs: inputs)
Results:
`
O
perating System: macOS
CPU Information: Intel(R) Core(TM) i7-8557U CPU @ 1.70GHz
Number of Available Cores: 8
Available memory: 16 GB
Elixir 1.9.4
Erlang 22.1
Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 0 ns
parallel: 1
inputs: none specified
Estimated total run time: 42 s
Benchmarking Lg: Elixir shit...
Benchmarking Lg: Iterators...
Benchmarking Md: Elixir shit...
Benchmarking Md: Iterators...
Benchmarking Sm: Elixir shit...
Benchmarking Sm: Iterators...
Name ips average deviation median 99th %
Sm: Iterators 585.08 K 1.71 μs ±1736.83% 0.90 μs 2.90 μs
Sm: Elixir shit 485.71 K 2.06 μs ±1265.29% 1.90 μs 3.90 μs
Md: Elixir shit 1.05 K 948.42 μs ±41.85% 853.90 μs 1861.86 μs
Md: Iterators 0.84 K 1191.63 μs ±30.73% 1067.90 μs 2352.18 μs
Lg: Iterators 0.00054 K 1846101.12 μs ±34.77% 1636249.48 μs 2743530.71 μs
Lg: Elixir shit 0.00049 K 2030950.57 μs ±10.21% 1921483.17 μs 2270112.40 μs
Comparison:
Sm: Iterators 585.08 K
Sm: Elixir shit 485.71 K - 1.20x slower +0.35 μs
Md: Elixir shit 1.05 K - 554.90x slower +946.71 μs
Md: Iterators 0.84 K - 697.19x slower +1189.92 μs
Lg: Iterators 0.00054 K - 1080111.45x slower +1846099.41 μs
Lg: Elixir shit 0.00049 K - 1188262.62x slower +2030948.86 μs
virviil@virviil bencher % mix run maps.exs
** (SyntaxError) maps.exs:20: syntax error before: "Medium map"
(elixir) lib/code.ex:813: Code.require_file/2
(mix) lib/mix/tasks/run.ex:145:
Mix.Tasks.Run.run/5 (mix) lib/mix/tasks/run.ex:85:
Mix.Tasks.Run.run/1 (mix) lib/mix/task.ex:331: Mix.Task.run_task/3
(mix) lib/mix/cli.ex:79: Mix.CLI.run_task/2
virviil@virviil bencher % mix run maps.exs
** (SyntaxError) maps.exs:16: syntax error before: ','
(elixir) lib/code.ex:813: Code.require_file/2
(mix) lib/mix/tasks/run.ex:145:
Mix.Tasks.Run.run/5 (mix) lib/mix/tasks/run.ex:85:
Mix.Tasks.Run.run/1 (mix) lib/mix/task.ex:331: Mix.Task.run_task/3
(mix) lib/mix/cli.ex:79: Mix.CLI.run_task/2
virviil@virviil bencher % mix run maps.exs
** (CompileError) maps.exs:1: undefined function efmodule/2
(elixir) lib/code.ex:813: Code.require_file/2
virviil@virviil bencher % mix run maps.exs
Operating System: macOS
CPU Information: Intel(R) Core(TM) i7-8557U CPU @ 1.70GHz
Number of Available Cores: 8
Available memory: 16 GB
Elixir 1.9.4
Erlang 22.1
Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 0 ns
parallel: 1
inputs: Large map, Medium map, Small map
Estimated total run time: 42 s
Benchmarking Elixir shit with input Large map...
Benchmarking Elixir shit with input Medium map...
Benchmarking Elixir shit with input Small map...
Benchmarking Iterators with input Large map...
Benchmarking Iterators with input Medium map...
Benchmarking Iterators with input Small map...
##### With input Large map #####
Name ips average deviation median 99th %