Skip to content

Instantly share code, notes, and snippets.

@egonSchiele egonSchiele/bench.rb
Last active Nov 11, 2017

Embed
What would you like to do?
Benchmarking OpenStruct alternatives
require 'benchmark'
require 'ostruct'
require 'rubygems'
require 'deep_struct'
require 'classy_struct'
require 'structure'
require 'deepopenstruct'
require 'recursive-open-struct'
require './fast_struct/lib/fast_struct'
REP = 100000
GET = ENV['GET']
Benchmark.bm 20 do |x|
x.report 'FastStruct' do
REP.times do |index|
h = FastStruct.new(:name => "User", :age => 21)
100.times { h.name } if GET
end
end
x.report 'Plain ol Hash' do
REP.times do |index|
h = Hash.new(:name => "User", :age => 21)
100.times { h[:name] } if GET
end
end
x.report 'Struct' do
REP.times do |index|
user = Struct.new(:name, :age)
h = user.new("User", 21)
100.times { h.name } if GET
end
end
x.report 'OpenStruct' do
REP.times do |index|
h = OpenStruct.new(:name => "User", :age => 21)
100.times { h.name } if GET
end
end
x.report 'Structure' do
REP.times do |index|
h = Structure.new(:name => "User", :age => 21)
100.times { h.name } if GET
end
end
x.report 'RecursiveOpenStruct' do
REP.times do |index|
h = RecursiveOpenStruct.new(:name => "User", :age => 21)
100.times { h.name } if GET
end
end
x.report 'DeepStruct' do
REP.times do |index|
h = DeepStruct.from_data(:name => "User", :age => 21)
100.times { h.name } if GET
end
end
x.report 'ClassyStruct' do
REP.times do |index|
classy = ClassyStruct.new
h = classy.new(:name => "User", :age => 21)
100.times { h.name } if GET
end
end
x.report 'DeepOpenStruct' do
REP.times do |index|
h = DeepOpenStruct.load(:name => "User", :age => 21)
100.times { h.name } if GET
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.