Created
June 9, 2020 14:39
-
-
Save cdegroot/26cab1b3a53b3dd9df508603a308e8d0 to your computer and use it in GitHub Desktop.
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 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