Skip to content

Instantly share code, notes, and snippets.

@philss

philss/tuple_struc_bench.exs Secret

Created Nov 19, 2018
Embed
What would you like to do?
A comparison for updating tuples and strucs
defmodule TupleStructBench do
defstruct name: nil, id: nil
def update_struct(struct = %__MODULE__{}, new_name) do
%{struct | name: new_name}
end
def update_tuple({id, _name}, new_name) do
{id, new_name}
end
end
my_struct = %TupleStructBench{id: 42, name: ""}
my_tuple = {42, ""}
inputs = %{
"Small string" => Base.encode64(:crypto.strong_rand_bytes(1000)),
"Middle string" => Base.encode64(:crypto.strong_rand_bytes(1_000_000)),
"Big" => Base.encode64(:crypto.strong_rand_bytes(1_000_000_000))
}
Benchee.run %{
"update name in struct" => fn(string) -> TupleStructBench.update_struct(my_struct, string) end,
"update name in tuple" => fn(string) -> TupleStructBench.update_tuple(my_tuple, string) end
}, time: 25, warmup: 10, inputs: inputs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.