Created
November 7, 2018 15:35
-
-
Save feymartynov/23a4652826dfccc5c042747ff5b067c0 to your computer and use it in GitHub Desktop.
Lambda vs private function benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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