Skip to content

Instantly share code, notes, and snippets.

@pcreux
Last active June 19, 2023 22:37
Show Gist options
  • Save pcreux/63a76bf36fc13525fc49bba191d4a88c to your computer and use it in GitHub Desktop.
Save pcreux/63a76bf36fc13525fc49bba191d4a88c to your computer and use it in GitHub Desktop.

Revisions

  1. pcreux renamed this gist Nov 15, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. pcreux revised this gist Nov 15, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions benchmark.rb
    Original file line number Diff line number Diff line change
    @@ -32,6 +32,6 @@ module Types
    end

    Benchmark.memory do |x|
    x.report("ActiveModel") { 1000.times { AMUser.new(attributes) } }
    x.report("DryStruct") { 1000.times { DryUser.new(attributes) }}
    x.report("ActiveModel") { AMUser.new(attributes) }
    x.report("DryStruct") { DryUser.new(attributes) }
    end
  3. pcreux revised this gist Nov 15, 2021. 2 changed files with 16 additions and 1 deletion.
    6 changes: 6 additions & 0 deletions benchmark.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    require 'active_model'
    require 'dry-struct'
    require 'benchmark/ips'
    require 'benchmark/memory'

    class AMUser
    include ActiveModel::Model
    @@ -28,4 +29,9 @@ module Types
    Benchmark.ips do |x|
    x.report("ActiveModel") { AMUser.new(attributes) }
    x.report("DryStruct") { DryUser.new(attributes) }
    end

    Benchmark.memory do |x|
    x.report("ActiveModel") { 1000.times { AMUser.new(attributes) } }
    x.report("DryStruct") { 1000.times { DryUser.new(attributes) }}
    end
    11 changes: 10 additions & 1 deletion results.txt
    Original file line number Diff line number Diff line change
    @@ -3,4 +3,13 @@ Warming up --------------------------------------
    DryStruct 26.361k i/100ms
    Calculating -------------------------------------
    ActiveModel 106.109k (± 6.9%) i/s - 539.231k in 5.107054s
    DryStruct 362.653k (± 6.4%) i/s - 1.819M in 5.040403s
    DryStruct 362.653k (± 6.4%) i/s - 1.819M in 5.040403s


    Calculating -------------------------------------
    ActiveModel 1,192 memsize ( 0.000 retained)
    20 objects ( 0.000 retained)
    8 strings ( 0.000 retained)
    DryStruct 208 memsize ( 0.000 retained)
    2 objects ( 0.000 retained)
    0 strings ( 0.000 retained)
  4. pcreux created this gist Nov 15, 2021.
    31 changes: 31 additions & 0 deletions benchmark.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    require 'active_model'
    require 'dry-struct'
    require 'benchmark/ips'

    class AMUser
    include ActiveModel::Model
    include ActiveModel::Attributes

    attribute :id, :integer
    attribute :name, :string
    attribute :birthday, :date
    attribute :last_logged_in_at, :datetime
    end

    class DryUser < Dry::Struct
    module Types
    include Dry::Types()
    end

    attribute :id, Types::Integer
    attribute :name, Types::String
    attribute :birthday, Types::Date
    attribute :last_logged_in_at, Types::Time
    end

    attributes = { id: 1, name: "Yuki", birthday: Date.new(1987, 12, 30), last_logged_in_at: Time.now }

    Benchmark.ips do |x|
    x.report("ActiveModel") { AMUser.new(attributes) }
    x.report("DryStruct") { DryUser.new(attributes) }
    end
    6 changes: 6 additions & 0 deletions results.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    Warming up --------------------------------------
    ActiveModel 11.473k i/100ms
    DryStruct 26.361k i/100ms
    Calculating -------------------------------------
    ActiveModel 106.109k (± 6.9%) i/s - 539.231k in 5.107054s
    DryStruct 362.653k (± 6.4%) i/s - 1.819M in 5.040403s