Skip to content

Instantly share code, notes, and snippets.

@weapp
Created February 27, 2017 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weapp/a6f0874fa53cc269d1fdd8e0bc330a23 to your computer and use it in GitHub Desktop.
Save weapp/a6f0874fa53cc269d1fdd8e0bc330a23 to your computer and use it in GitHub Desktop.
defmodule Mod do
@table %{
a: 1, b: 2, c: 3, d: 4, e: 5,
f: 6, g: 7, h: 8, i: 9, j: 0,
}
def fun1(k) do @table[k] end
for {k, v} <- @table do
def fun2(unquote(k)) do unquote(v) end
end
end
defmodule BasicBench do
use Benchfella
bench "map" do
1..10_000 |> Enum.each(fn _ ->
Mod.fun1(:a); Mod.fun1(:b); Mod.fun1(:c); Mod.fun1(:d); Mod.fun1(:e);
Mod.fun1(:f); Mod.fun1(:g); Mod.fun1(:h); Mod.fun1(:i); Mod.fun1(:j)
end)
end
bench "pattern matching" do
1..10_000 |> Enum.each(fn _ ->
Mod.fun2(:a); Mod.fun2(:b); Mod.fun2(:c); Mod.fun2(:d); Mod.fun2(:e);
Mod.fun2(:f); Mod.fun2(:g); Mod.fun2(:h); Mod.fun2(:i); Mod.fun2(:j)
end)
end
end
# Settings:
# duration: 1.0 s
# ## BasicBench
# [11:13:14] 1/2: map
# [11:13:16] 2/2: pattern matching
# Finished in 4.24 seconds
# ## BasicBench
# benchmark name iterations average time
# pattern matching 1000 1459.69 µs/op
# map 200 8446.38 µs/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment