Skip to content

Instantly share code, notes, and snippets.

@ventsislaf
Created June 21, 2016 13:13
Show Gist options
  • Save ventsislaf/d9a82d4de651b671d4b5d90e84555d19 to your computer and use it in GitHub Desktop.
Save ventsislaf/d9a82d4de651b671d4b5d90e84555d19 to your computer and use it in GitHub Desktop.
list = Enum.to_list(1..100_000)
Benchee.init(%{time: 3})
|> Benchee.benchmark("Enum.at", fn -> Enum.at(list, length(list) - 2) end)
|> Benchee.benchmark(
"revers + pattern matching",
fn ->
[_, next_to_last | _] = Enum.reverse(list)
next_to_last
end
)
|> Benchee.statistics
|> Benchee.Formatters.Console.format
|> IO.puts
# Results
Benchmarking Enum.at...
Benchmarking revers + pattern matching...
Name ips average deviation median
revers + pattern matching 2264.76 441.55μs (±74.31%) 257.00μs
Enum.at 962.23 1039.25μs (±15.16%) 980.00μs
Comparison:
revers + pattern matching 2264.76
Enum.at 962.23 - 2.35x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment