/tuple_struc_bench.exs Secret
Created
November 19, 2018 00:14
Star
You must be signed in to star a gist
A comparison for updating tuples and strucs
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 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