Skip to content

Instantly share code, notes, and snippets.

@Fryguy
Created June 21, 2018 16:06
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 Fryguy/ac9adffcc48d343fb30d1c0373819357 to your computer and use it in GitHub Desktop.
Save Fryguy/ac9adffcc48d343fb30d1c0373819357 to your computer and use it in GitHub Desktop.
MiqHashStruct vs OpenStruct
require 'benchmark/ips'
require 'manageiq-gems-pending'
require 'miq-hash_struct'
require 'ostruct'
large_hash = (0..1000).each_with_object({}) { |i, h| h["a#{i}"] = i }
small_hash = (0..4).each_with_object({}) { |i, h| h["a#{i}"] = i }
Benchmark.ips do |x|
x.report("MiqHashStruct.new-large") { MiqHashStruct.new(large_hash) }
x.report("OpenStruct.new-large") { OpenStruct.new(large_hash) }
x.report("MiqHashStruct.new-small") { MiqHashStruct.new(small_hash) }
x.report("OpenStruct.new-small") { OpenStruct.new(small_hash) }
end
puts "======================================================================"
large_hash_struct = MiqHashStruct.new(large_hash)
large_open_struct = OpenStruct.new(large_hash)
Benchmark.ips do |x|
x.report("MiqHashStruct high-read") { 1000.times { large_hash_struct.a0 } }
x.report("OpenStruct high-read") { 1000.times { large_open_struct.a0 } }
x.report("MiqHashStruct low-read") { large_hash_struct.a0 }
x.report("OpenStruct low_read") { large_open_struct.a0 }
end
Warming up --------------------------------------
MiqHashStruct.new-large
22.198k i/100ms
OpenStruct.new-large 437.000 i/100ms
MiqHashStruct.new-small
96.687k i/100ms
OpenStruct.new-small 40.280k i/100ms
Calculating -------------------------------------
MiqHashStruct.new-large
274.544k (±23.2%) i/s - 1.310M in 5.013370s
OpenStruct.new-large 4.372k (± 4.0%) i/s - 21.850k in 5.005783s
MiqHashStruct.new-small
1.279M (± 4.7%) i/s - 6.478M in 5.075022s
OpenStruct.new-small 423.267k (± 9.4%) i/s - 2.095M in 5.001133s
======================================================================
Warming up --------------------------------------
MiqHashStruct high-read
194.000 i/100ms
OpenStruct high-read 819.000 i/100ms
MiqHashStruct low-read
112.146k i/100ms
OpenStruct low_read 220.056k i/100ms
Calculating -------------------------------------
MiqHashStruct high-read
1.907k (± 7.3%) i/s - 9.506k in 5.013554s
OpenStruct high-read 8.319k (± 3.4%) i/s - 41.769k in 5.026756s
MiqHashStruct low-read
1.807M (± 6.1%) i/s - 9.084M in 5.047288s
OpenStruct low_read 6.422M (± 6.4%) i/s - 32.128M in 5.025546s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment