Last active
August 29, 2015 14:23
-
-
Save cedricpim/4bd588079a14d7b23f10 to your computer and use it in GitHub Desktop.
Revisions
-
Cedric Pimenta revised this gist
Jun 18, 2015 . 1 changed file with 29 additions and 28 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,8 @@ require 'benchmark' require 'ostruct' require 'memory_profiler' i = 100000 User = Struct.new(:name, :age) @@ -21,74 +21,75 @@ end Benchmark.bm 20 do |x| x.report 'OpenStruct slow' do i.times { |index| OpenStruct.new(name: "User", age: 21) } end x.report 'OpenStruct fast' do i.times { |index| OpenStruct.new(HASH) } end x.report 'Struct slow' do i.times { |index| User.new("User", 21) } end x.report 'Struct fast' do i.times { |index| User.new(USER, AGE) } end x.report 'Hash slow' do i.times { |index| Hash.new(user: "User", age: 21) } end x.report 'Hash fast' do i.times { |index| Hash.new(HASH) } end x.report 'Class slow' do i.times { |index| Poro.new("User", 21) } end x.report 'Class fast' do i.times { |index| Poro.new(USER, AGE) } end end j = 100 results = {} results[:openstruct_slow] = MemoryProfiler.report do (1..j).map { |index| OpenStruct.new(name: "User", age: 21) } end results[:openstruct_fast] = MemoryProfiler.report do (1..j).map { |index| OpenStruct.new(HASH) } end results[:struct_slow] = MemoryProfiler.report do (1..j).map { |index| User.new("User", 21) } end results[:struct_fast] = MemoryProfiler.report do (1..j).map { |index| User.new(USER, AGE) } end results[:hash_slow] = MemoryProfiler.report do (1..j).map { |index| Hash.new(user: "User", age: 21) } end results[:hash_fast] = MemoryProfiler.report do (1..j).map { |index| Hash.new(HASH) } end results[:klass_slow] = MemoryProfiler.report do (1..j).map { |index| Poro.new("User", 21) } end 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 result.pretty_print end -
Cedric Pimenta revised this gist
Jun 18, 2015 . 1 changed file with 50 additions and 25 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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 class Poro attr_accessor :user, :age @@ -20,50 +21,74 @@ end Benchmark.bm 20 do |x| x.report 'OpenStruct slow' do REP.times { |index| OpenStruct.new(name: "User", age: 21) } end x.report 'OpenStruct fast' do REP.times { |index| OpenStruct.new(HASH) } end x.report 'Struct slow' do REP.times { |index| User.new("User", 21) } end x.report 'Struct fast' do REP.times { |index| User.new(USER, AGE) } end x.report 'Hash slow' do REP.times { |index| Hash.new(user: "User", age: 21) } end x.report 'Hash fast' do REP.times { |index| Hash.new(HASH) } end x.report 'Class slow' do REP.times { |index| Poro.new("User", 21) } end x.report 'Class fast' do 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 -
Cedric Pimenta created this gist
Jun 18, 2015 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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