Skip to content

Instantly share code, notes, and snippets.

@feymartynov
Created November 7, 2018 15:35
Show Gist options
  • Save feymartynov/23a4652826dfccc5c042747ff5b067c0 to your computer and use it in GitHub Desktop.
Save feymartynov/23a4652826dfccc5c042747ff5b067c0 to your computer and use it in GitHub Desktop.
Lambda vs private function benchmark
defmodule Test1 do
def foo(a) do
bar = fn x ->
x + 1
end
bar.(a)
end
end
defmodule Test2 do
def foo(a) do
bar(a)
end
defp bar(x) do
x + 1
end
end
defmodule Benchmark do
def measure(function) do
function
|> :timer.tc()
|> elem(0)
|> Kernel./(1_000_000)
end
end
Benchmark.measure(fn ->
for i <- 1..10_000_000, do: Test1.foo(i)
end) |> IO.inspect(label: "Lambda")
Benchmark.measure(fn ->
for i <- 1..10_000_000, do: Test2.foo(i)
end) |> IO.inspect(label: "Private function")
# Lambda: 2.099491
# Private function: 0.698419
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment