Skip to content

Instantly share code, notes, and snippets.

@philss
Created November 19, 2018 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philss/58f56088db6d0334178ebed542eb422b to your computer and use it in GitHub Desktop.
Save philss/58f56088db6d0334178ebed542eb422b to your computer and use it in GitHub Desktop.
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