Skip to content

Instantly share code, notes, and snippets.

@skatkov
Forked from palexander/ruby_data_object_comparison.rb
Last active September 17, 2020 12:24
Show Gist options
  • Select an option

  • Save skatkov/c32ffff81dc22e2e955533e4591b335c to your computer and use it in GitHub Desktop.

Select an option

Save skatkov/c32ffff81dc22e2e955533e4591b335c to your computer and use it in GitHub Desktop.

Revisions

  1. Stanislav (Stas) Katkov revised this gist Sep 17, 2020. 1 changed file with 13 additions and 15 deletions.
    28 changes: 13 additions & 15 deletions ruby_data_object_comparison.rb
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,10 @@
    EMAIL = "[email protected]"

    class Person
    attr_accessor :name, :email
    def initialize(name:, email:)
    @name = name
    @email = email
    end
    end

    Benchmark.bm(13) do |x|
    @@ -18,32 +21,27 @@ class Person

    x.report("openstruct:") do
    COUNT.times do
    p = OpenStruct.new
    p.name = NAME
    p.email = EMAIL
    OpenStruct.new(name: NAME, email: EMAIL)
    end
    end

    x.report("struct:") do
    PersonStruct = Struct.new(:name, :email)
    COUNT.times do
    p = PersonStruct.new
    p.name = NAME
    p.email = EMAIL
    PersonStruct.new(name: NAME, email: EMAIL)
    end
    end

    x.report("class:") do
    COUNT.times do
    p = Person.new
    p.name = NAME
    p.email = EMAIL
    Person.new(name: NAME, email: EMAIL)
    end
    end
    end

    user system total real
    hash: 4.610000 0.250000 4.860000 ( 4.857328)
    openstruct: 134.900000 2.830000 137.730000 (137.811739)
    struct: 3.940000 0.000000 3.940000 ( 3.943897)
    class: 2.480000 0.000000 2.480000 ( 2.486405)

    user system total real
    hash: 1.310324 0.000000 1.310324 ( 1.313880)
    openstruct: 6.497740 0.002865 6.500605 ( 6.514609)
    struct: 3.147043 0.000004 3.147047 ( 3.154298)
    class: 5.571384 0.000000 5.571384 ( 5.583647)
  2. @palexander palexander revised this gist Jan 24, 2014. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion ruby_data_object_comparison.rb
    Original file line number Diff line number Diff line change
    @@ -40,4 +40,10 @@ class Person
    p.email = EMAIL
    end
    end
    end
    end

    user system total real
    hash: 4.610000 0.250000 4.860000 ( 4.857328)
    openstruct: 134.900000 2.830000 137.730000 (137.811739)
    struct: 3.940000 0.000000 3.940000 ( 3.943897)
    class: 2.480000 0.000000 2.480000 ( 2.486405)
  3. @palexander palexander created this gist Jan 24, 2014.
    43 changes: 43 additions & 0 deletions ruby_data_object_comparison.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    require 'ostruct'
    require 'benchmark'

    COUNT = 10_000_000
    NAME = "Test Name"
    EMAIL = "[email protected]"

    class Person
    attr_accessor :name, :email
    end

    Benchmark.bm(13) do |x|
    x.report("hash:") do
    COUNT.times do
    p = {name: NAME, email: EMAIL}
    end
    end

    x.report("openstruct:") do
    COUNT.times do
    p = OpenStruct.new
    p.name = NAME
    p.email = EMAIL
    end
    end

    x.report("struct:") do
    PersonStruct = Struct.new(:name, :email)
    COUNT.times do
    p = PersonStruct.new
    p.name = NAME
    p.email = EMAIL
    end
    end

    x.report("class:") do
    COUNT.times do
    p = Person.new
    p.name = NAME
    p.email = EMAIL
    end
    end
    end