Skip to content

Instantly share code, notes, and snippets.

@pma
Created March 26, 2014 09:39
Show Gist options
  • Save pma/9779746 to your computer and use it in GitHub Desktop.
Save pma/9779746 to your computer and use it in GitHub Desktop.
defmodule Bench do
@sample 100_000
defp average(time) do
time / @sample
end
defp bench(fun) do
f = fn ->
Enum.each 1..@sample, fn _ -> fun.() end
end
pid = spawn_link(fn ->
:timer.tc(f, []) |> elem(0) |> average |> IO.inspect
end)
receive do: ({ :EXIT, ^pid, :normal } -> :ok)
end
def run do
buf = :crypto.rand_bytes(2_000)
b64 = :base64.encode(buf)
IO.puts ":base64.encode"
bench fn -> :base64.encode(buf) end
IO.puts ""
IO.puts ":base64.decode"
bench fn -> :base64.decode(b64) end
IO.puts ""
IO.puts "Base.encode64"
bench fn -> Base.encode64(buf) end
IO.puts ""
IO.puts "Base.decode64"
bench fn -> Base.decode64(b64) end
IO.puts ""
end
end
Bench.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment