Skip to content

Instantly share code, notes, and snippets.

@yorickpeterse
Created June 16, 2015 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yorickpeterse/d2cc45fc7e9117bb8886 to your computer and use it in GitHub Desktop.
Save yorickpeterse/d2cc45fc7e9117bb8886 to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
require 'ostruct'
struct = Struct.new(:name)
Benchmark.ips do |bench|
bench.report 'Struct' do
struct.new(:name => 'Alice')
end
bench.report 'Struct inline' do
Struct.new(:name).new(:name => 'Alice')
end
bench.report 'OpenStruct' do
OpenStruct.new(:name => 'Alice')
end
bench.compare!
end
Calculating -------------------------------------
Struct 69.264k i/100ms
Struct inline 8.916k i/100ms
OpenStruct 11.488k i/100ms
-------------------------------------------------
Struct 1.300M (± 3.2%) i/s - 6.511M
Struct inline 89.434k (±13.2%) i/s - 445.800k
OpenStruct 133.604k (± 6.1%) i/s - 666.304k
Comparison:
Struct: 1299835.3 i/s
OpenStruct: 133604.2 i/s - 9.73x slower
Struct inline: 89434.1 i/s - 14.53x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment