Skip to content

Instantly share code, notes, and snippets.

@slavone
Created May 1, 2018 10:47
Show Gist options
  • Save slavone/3bb2c07fab65052fe205747a99082417 to your computer and use it in GitHub Desktop.
Save slavone/3bb2c07fab65052fe205747a99082417 to your computer and use it in GitHub Desktop.
Benchmarking using try/rescue agains function_exported? in elixir for handling unreliable function calls
defmodule Generator do
defmacro testing do
names = (1..10000)
|> Enum.map(fn(_) ->
:crypto.strong_rand_bytes(10) |> Base.url_encode64 |> binary_part(0, 10)
end)
|> Enum.map(&String.to_atom/1)
names
|> Enum.take_every(2)
|> Enum.map(fn(name) ->
quote do
def unquote(name)(), do: unquote(Atom.to_string(name))
end
end)
|> List.insert_at(0, quote do
def functions, do: unquote(names)
end)
end
end
defmodule Benchmark do
require Generator
Generator.testing
def test do
Benchee.run(%{
"try rescue" => fn ->
Enum.each functions, fn(name) ->
try do
apply(__MODULE__, name, [])
rescue
_ -> nil
end
end
end,
"function_exported?" => fn ->
Enum.each functions, fn(name) ->
if function_exported?(__MODULE__, name, 0) do
apply(__MODULE__, name, [])
else
nil
end
end
end
})
end
end
# iex(1)> Benchmark.test
# Operating System: macOS"
# CPU Information: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
# Number of Available Cores: 8
# Available memory: 16 GB
# Elixir 1.6.0
# Erlang 20.2.2
#
# Benchmark suite executing with the following configuration:
# warmup: 2 s
# time: 5 s
# memory time: 0 μs
# parallel: 1
# inputs: none specified
# Estimated total run time: 14 s
#
#
# Benchmarking function_exported?...
# Benchmarking try rescue...
#
# Name ips average deviation median 99th %
# function_exported? 854.95 1.17 ms ±13.33% 1.12 ms 1.89 ms
# try rescue 36.80 27.18 ms ±7.11% 26.67 ms 33.78 ms
#
# Comparison:
# function_exported? 854.95
# try rescue 36.80 - 23.23x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment