Created
June 11, 2015 11:57
-
-
Save workmad3/25e4a0a617a7e3639d14 to your computer and use it in GitHub Desktop.
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 characters
require 'ostruct' | |
require 'benchmark' | |
COUNT = 10_000_000 | |
NAME = "Test Name" | |
EMAIL = "[email protected]" | |
class SimpleOstruct | |
def initialize(attrs = {}) | |
@attrs = attrs | |
end | |
def method_missing(name, *args) | |
if name.to_s =~ /=$/ | |
@attrs[name.to_s[0..-2]] = args.first | |
else | |
@attrs[name] = args.first | |
end | |
end | |
end | |
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("simple_openstruct:") do | |
COUNT.times do | |
p = SimpleOstruct.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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment