Skip to content

Instantly share code, notes, and snippets.

@pragtobgists
pragtobgists / new_run.exs
Created December 1, 2016 14:21
New benchee API
list = Enum.to_list(1..10_000)
map_fun = fn(i) -> [i, i * i] end
Benchee.run(%{
"flat_map" => fn -> Enum.flat_map(list, map_fun) end,
"map.flatten" => fn -> list |> Enum.map(map_fun) |> List.flatten end
},
formatters: [
&Benchee.Formatters.HTML.output/1,
&Benchee.Formatters.Console.output/1
@pragtobgists
pragtobgists / deep_merge.exs
Created November 10, 2016 19:14
deep_merge in action
DeepMerge.deep_merge(%{a: 1, b: [x: 10, y: 9]}, %{b: [y: 20, z: 30], c: 4})
# => %{a: 1, b: [x: 10, y: 20, z: 30], c: 4}
@pragtobgists
pragtobgists / benchmark.exs
Created October 13, 2016 08:04
Run with smallest scaling
list = Enum.to_list(1..10_000)
map_fun = fn(i) -> [i, i * i] end
Benchee.run(%{time: 3, console: %{unit_scaling: :smallest}}, %{
"flat_map" => fn -> Enum.flat_map(list, map_fun) end,
"map.flatten" => fn -> list |> Enum.map(map_fun) |> List.flatten end
})
@pragtobgists
pragtobgists / benchmark.exs
Created October 13, 2016 08:00
Unit Scaling in benchee in action
list = Enum.to_list(1..10_000)
map_fun = fn(i) -> [i, i * i] end
Benchee.run(%{time: 3}, %{
"flat_map" => fn -> Enum.flat_map(list, map_fun) end,
"map.flatten" => fn -> list |> Enum.map(map_fun) |> List.flatten end
})
@pragtobgists
pragtobgists / run_parallel.exs
Created July 12, 2016 07:06
Parallel benchee benchmark executing each job with a parallelity level of 2
list = Enum.to_list(1..10_000)
map_fun = fn(i) -> [i, i * i] end
Benchee.run(%{time: 3, parallel: 2}, %{
"flat_map" => fn -> Enum.flat_map(list, map_fun) end,
"map.flatten" => fn -> list |> Enum.map(map_fun) |> List.flatten end
})
@pragtobgists
pragtobgists / multiple_formatters.exs
Last active July 12, 2016 07:07
Mutliple formatters in benchee
list = Enum.to_list(1..10_000)
map_fun = fn(i) -> [i, i * i] end
Benchee.run(
%{
formatters: [
&Benchee.Formatters.CSV.output/1,
&Benchee.Formatters.Console.output/1
],
csv: %{file: "my.csv"}
@pragtobgists
pragtobgists / number.ex
Last active February 26, 2017 10:33
very dumb is even function benchmarked tco vs. no tco
defmodule Number do
def is_even?(0), do: true
def is_even?(n) do
!is_even?(n - 1)
end
def is_even_tco?(n, acc \\ true)
def is_even_tco?(0, acc), do: acc
def is_even_tco?(n, acc) do
is_even_tco?(n - 1, !acc)
@pragtobgists
pragtobgists / no_tco_list.exs
Last active June 24, 2016 07:45
Running map implementations on a large list to see memory consumption
list = Enum.to_list(1..100_000_000)
MyMap.map_body(list, fn(i) -> i + 1 end)
@pragtobgists
pragtobgists / my_map_bench.exs
Last active February 26, 2017 10:35
Benchmark of different map implementations
list = Enum.to_list(1..10_000)
map_fun = fn(i) -> i + 1 end
Benchee.run %{
"map tail-recursive with ++" =>
fn -> MyMap.map_tco_concat(list, map_fun) end,
"map with TCO reverse" =>
fn -> MyMap.map_tco(list, map_fun) end,
"stdlib map" =>
fn -> Enum.map(list, map_fun) end,
@pragtobgists
pragtobgists / my_map.ex
Last active June 24, 2016 07:08
Map implementations both with TCO and without
defmodule MyMap do
def map_tco(list, function) do
Enum.reverse _map_tco([], list, function)
end
defp _map_tco(acc, [head | tail], function) do
_map_tco([function.(head) | acc], tail, function)
end
defp _map_tco(acc, [], _function) do