Skip to content

Instantly share code, notes, and snippets.

@michalmuskala
Created September 30, 2015 23:07
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 michalmuskala/6a52648a72261220ccc8 to your computer and use it in GitHub Desktop.
Save michalmuskala/6a52648a72261220ccc8 to your computer and use it in GitHub Desktop.
defmodule MapBench do
use Benchfella
def map_cons([], _fun), do: []
def map_cons([head | tail], fun), do: [fun.(head) | map_cons(tail, fun)]
def map_recurse(list, fun, acc \\ [])
def map_recurse([], _fun, acc), do: :lists.reverse(acc)
def map_recurse([head | tail], fun, acc), do: map_recurse(tail, fun, [fun.(head) | acc])
def map_for(list, fun), do: for i <- list, do: fun.(i)
@list100 1..100 |> Enum.to_list
@list1000 1..1000 |> Enum.to_list
@list10000 1..10000 |> Enum.to_list
@list100000 1..100000 |> Enum.to_list
bench "cons 100" do
@list100 |> map_cons(fn x -> x * 2 end)
end
bench "cons 1000" do
@list1000 |> map_cons(fn x -> x * 2 end)
end
bench "cons 10000" do
@list10000 |> map_cons(fn x -> x * 2 end)
end
bench "cons 100000" do
@list100000 |> map_cons(fn x -> x * 2 end)
end
bench "recurse 100" do
@list100 |> map_recurse(fn x -> x * 2 end)
end
bench "recurse 1000" do
@list1000 |> map_recurse(fn x -> x * 2 end)
end
bench "recurse 10000" do
@list10000 |> map_recurse(fn x -> x * 2 end)
end
bench "recurse 100000" do
@list100000 |> map_recurse(fn x -> x * 2 end)
end
bench "for 100" do
@list100 |> map_for(fn x -> x * 2 end)
end
bench "for 1000" do
@list1000 |> map_for(fn x -> x * 2 end)
end
bench "for 10000" do
@list10000 |> map_for(fn x -> x * 2 end)
end
bench "for 100000" do
@list100000 |> map_for(fn x -> x * 2 end)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment