Last active
August 29, 2015 14:16
-
-
Save rklemme/051c861a9ea66dcce538 to your computer and use it in GitHub Desktop.
Benchmark of creating 1,000,000 objects of a Struct generated class, OpenStruct and Hash
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
#!/usr/bin/ruby | |
require 'benchmark' | |
require 'ostruct' | |
REP = 10**6 | |
Cl = Struct.new :name, :age | |
Benchmark.bmbm 20 do |x| | |
x.report "struct" do | |
REP.times { Cl.new "User", 21 } | |
end | |
x.report "open struct with hash" do | |
REP.times { OpenStruct.new(:user => "User", :age => 21) } | |
end | |
x.report "open struct no hash" do | |
REP.times { | |
os = OpenStruct.new | |
os.user = "User" | |
os.age = 21 | |
} | |
end | |
x.report "hash" do | |
REP.times { {:name => "User" , :age => 21} } | |
end | |
end |
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
$ ruby -v | |
ruby 2.0.0p598 (2014-11-13) [x86_64-cygwin] | |
$ ./measure-struct-vs-hash.rb | |
Rehearsal --------------------------------------------------------- | |
struct 0.281000 0.000000 0.281000 ( 0.283016) | |
open struct with hash 11.076000 0.000000 11.076000 ( 11.068633) | |
open struct no hash 12.605000 0.000000 12.605000 ( 12.605721) | |
hash 0.577000 0.000000 0.577000 ( 0.589034) | |
----------------------------------------------- total: 24.539000sec | |
user system total real | |
struct 0.281000 0.000000 0.281000 ( 0.291017) | |
open struct with hash 11.154000 0.000000 11.154000 ( 11.145637) | |
open struct no hash 12.574000 0.000000 12.574000 ( 12.575719) | |
hash 0.546000 0.000000 0.546000 ( 0.549031) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment