Skip to content

Instantly share code, notes, and snippets.

@cedricpim
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save cedricpim/4bd588079a14d7b23f10 to your computer and use it in GitHub Desktop.

Select an option

Save cedricpim/4bd588079a14d7b23f10 to your computer and use it in GitHub Desktop.

Revisions

  1. Cedric Pimenta revised this gist Jun 18, 2015. 1 changed file with 29 additions and 28 deletions.
    57 changes: 29 additions & 28 deletions struct_hash_ostruct_class
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    require 'benchmark'
    require 'ostruct'
    require 'ruby-prof'
    require 'memory_profiler'

    REP = 100000
    i = 100000

    User = Struct.new(:name, :age)

    @@ -21,74 +21,75 @@ end

    Benchmark.bm 20 do |x|
    x.report 'OpenStruct slow' do
    REP.times { |index| OpenStruct.new(name: "User", age: 21) }
    i.times { |index| OpenStruct.new(name: "User", age: 21) }
    end

    x.report 'OpenStruct fast' do
    REP.times { |index| OpenStruct.new(HASH) }
    i.times { |index| OpenStruct.new(HASH) }
    end

    x.report 'Struct slow' do
    REP.times { |index| User.new("User", 21) }
    i.times { |index| User.new("User", 21) }
    end

    x.report 'Struct fast' do
    REP.times { |index| User.new(USER, AGE) }
    i.times { |index| User.new(USER, AGE) }
    end

    x.report 'Hash slow' do
    REP.times { |index| Hash.new(user: "User", age: 21) }
    i.times { |index| Hash.new(user: "User", age: 21) }
    end

    x.report 'Hash fast' do
    REP.times { |index| Hash.new(HASH) }
    i.times { |index| Hash.new(HASH) }
    end

    x.report 'Class slow' do
    REP.times { |index| Poro.new("User", 21) }
    i.times { |index| Poro.new("User", 21) }
    end

    x.report 'Class fast' do
    REP.times { |index| Poro.new(USER, AGE) }
    i.times { |index| Poro.new(USER, AGE) }
    end
    end

    j = 100

    results = {}
    results[:openstruct_slow] = RubyProf.profile do
    REP.times { |index| OpenStruct.new(name: "User", age: 21) }
    results[:openstruct_slow] = MemoryProfiler.report do
    (1..j).map { |index| OpenStruct.new(name: "User", age: 21) }
    end

    results[:openstruct_fast] = RubyProf.profile do
    REP.times { |index| OpenStruct.new(HASH) }
    results[:openstruct_fast] = MemoryProfiler.report do
    (1..j).map { |index| OpenStruct.new(HASH) }
    end

    results[:struct_slow] = RubyProf.profile do
    REP.times { |index| User.new("User", 21) }
    results[:struct_slow] = MemoryProfiler.report do
    (1..j).map { |index| User.new("User", 21) }
    end

    results[:struct_fast] = RubyProf.profile do
    REP.times { |index| User.new(USER, AGE) }
    results[:struct_fast] = MemoryProfiler.report do
    (1..j).map { |index| User.new(USER, AGE) }
    end

    results[:hash_slow] = RubyProf.profile do
    REP.times { |index| Hash.new(user: "User", age: 21) }
    results[:hash_slow] = MemoryProfiler.report do
    (1..j).map { |index| Hash.new(user: "User", age: 21) }
    end

    results[:hash_fast] = RubyProf.profile do
    REP.times { |index| Hash.new(HASH) }
    results[:hash_fast] = MemoryProfiler.report do
    (1..j).map { |index| Hash.new(HASH) }
    end

    results[:klass_slow] = RubyProf.profile do
    REP.times { |index| Poro.new("User", 21) }
    results[:klass_slow] = MemoryProfiler.report do
    (1..j).map { |index| Poro.new("User", 21) }
    end

    results[:klass_fast] = RubyProf.profile do
    REP.times { |index| Poro.new(USER, AGE) }
    results[:klass_fast] = MemoryProfiler.report do
    (1..j).map { |index| Poro.new(USER, AGE) }
    end

    # Print a graph profile to text
    results.each_pair do |type, result|
    puts type
    printer = RubyProf::GraphPrinter.new(result)
    printer.print(STDOUT, {})
    result.pretty_print
    end
  2. Cedric Pimenta revised this gist Jun 18, 2015. 1 changed file with 50 additions and 25 deletions.
    75 changes: 50 additions & 25 deletions struct_hash_ostruct_class
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,14 @@
    require 'benchmark'
    require 'ostruct'
    require 'ruby-prof'

    REP = 100000

    User = Struct.new(:name, :age)

    USER = "User".freeze
    AGE = 21
    HASH = {:name => USER, :age => AGE}.freeze
    HASH = { name: USER, age: AGE}.freeze

    class Poro
    attr_accessor :user, :age
    @@ -20,50 +21,74 @@ end

    Benchmark.bm 20 do |x|
    x.report 'OpenStruct slow' do
    REP.times do |index|
    OpenStruct.new(:name => "User", :age => 21)
    end
    REP.times { |index| OpenStruct.new(name: "User", age: 21) }
    end

    x.report 'OpenStruct fast' do
    REP.times do |index|
    OpenStruct.new(HASH)
    end
    REP.times { |index| OpenStruct.new(HASH) }
    end

    x.report 'Struct slow' do
    REP.times do |index|
    User.new("User", 21)
    end
    REP.times { |index| User.new("User", 21) }
    end

    x.report 'Struct fast' do
    REP.times do |index|
    User.new(USER, AGE)
    end
    REP.times { |index| User.new(USER, AGE) }
    end

    x.report 'Hash slow' do
    REP.times do |index|
    Hash.new(user: "User", age: 21)
    end
    REP.times { |index| Hash.new(user: "User", age: 21) }
    end

    x.report 'Hash fast' do
    REP.times do |index|
    Hash.new(user: USER, age: AGE)
    end
    REP.times { |index| Hash.new(HASH) }
    end

    x.report 'Class slow' do
    REP.times do |index|
    Poro.new("User", 21)
    end
    REP.times { |index| Poro.new("User", 21) }
    end

    x.report 'Class fast' do
    REP.times do |index|
    Poro.new(USER, AGE)
    end
    REP.times { |index| Poro.new(USER, AGE) }
    end
    end

    results = {}
    results[:openstruct_slow] = RubyProf.profile do
    REP.times { |index| OpenStruct.new(name: "User", age: 21) }
    end

    results[:openstruct_fast] = RubyProf.profile do
    REP.times { |index| OpenStruct.new(HASH) }
    end

    results[:struct_slow] = RubyProf.profile do
    REP.times { |index| User.new("User", 21) }
    end

    results[:struct_fast] = RubyProf.profile do
    REP.times { |index| User.new(USER, AGE) }
    end

    results[:hash_slow] = RubyProf.profile do
    REP.times { |index| Hash.new(user: "User", age: 21) }
    end

    results[:hash_fast] = RubyProf.profile do
    REP.times { |index| Hash.new(HASH) }
    end

    results[:klass_slow] = RubyProf.profile do
    REP.times { |index| Poro.new("User", 21) }
    end

    results[:klass_fast] = RubyProf.profile do
    REP.times { |index| Poro.new(USER, AGE) }
    end

    # Print a graph profile to text
    results.each_pair do |type, result|
    puts type
    printer = RubyProf::GraphPrinter.new(result)
    printer.print(STDOUT, {})
    end
  3. Cedric Pimenta created this gist Jun 18, 2015.
    69 changes: 69 additions & 0 deletions struct_hash_ostruct_class
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    require 'benchmark'
    require 'ostruct'

    REP = 100000

    User = Struct.new(:name, :age)

    USER = "User".freeze
    AGE = 21
    HASH = {:name => USER, :age => AGE}.freeze

    class Poro
    attr_accessor :user, :age

    def initialize(user, age)
    @user = user
    @age = age
    end
    end

    Benchmark.bm 20 do |x|
    x.report 'OpenStruct slow' do
    REP.times do |index|
    OpenStruct.new(:name => "User", :age => 21)
    end
    end

    x.report 'OpenStruct fast' do
    REP.times do |index|
    OpenStruct.new(HASH)
    end
    end

    x.report 'Struct slow' do
    REP.times do |index|
    User.new("User", 21)
    end
    end

    x.report 'Struct fast' do
    REP.times do |index|
    User.new(USER, AGE)
    end
    end

    x.report 'Hash slow' do
    REP.times do |index|
    Hash.new(user: "User", age: 21)
    end
    end

    x.report 'Hash fast' do
    REP.times do |index|
    Hash.new(user: USER, age: AGE)
    end
    end

    x.report 'Class slow' do
    REP.times do |index|
    Poro.new("User", 21)
    end
    end

    x.report 'Class fast' do
    REP.times do |index|
    Poro.new(USER, AGE)
    end
    end
    end