Skip to content

Instantly share code, notes, and snippets.

@cdegroot
Created June 9, 2020 14:39
Show Gist options
  • Save cdegroot/26cab1b3a53b3dd9df508603a308e8d0 to your computer and use it in GitHub Desktop.
Save cdegroot/26cab1b3a53b3dd9df508603a308e8d0 to your computer and use it in GitHub Desktop.
defmodule User do
@defaults name: nil, age: 0
defstruct @defaults
import Record
defrecord(:user, @defaults)
end
defmodule Bench do
def run do
import User
loops = 1..10000000
user_struct = %User{name: "John", age: 1}
user_record = user(name: "John", age: 1)
IO.inspect(
:timer.tc(fn ->
Enum.reduce(loops, user_struct, fn i, u ->
%User{u | age: i}
end)
end))
IO.inspect(
:timer.tc(fn ->
Enum.reduce(loops, user_record, fn i, u ->
user(u, age: i)
end)
end))
end
end
Enum.each(1..5, fn _ -> Bench.run end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment