Skip to content

Instantly share code, notes, and snippets.

@gfmurphy
Created March 6, 2016 13:05
Show Gist options
  • Save gfmurphy/1bfa1b34ec356a294f5b to your computer and use it in GitHub Desktop.
Save gfmurphy/1bfa1b34ec356a294f5b to your computer and use it in GitHub Desktop.
OpenStruct Performance Test
require "benchmark"
require "ostruct"
BadContext = Class.new(OpenStruct)
class BetterContext
attr_accessor :a, :b, :c, :d, :e, :f
end
def test_it(klass, &b)
10_000.times do
context = klass.new
("a".."f").each do |value|
yield context, value
end
end
end
Benchmark.bm do |x|
x.report("bad context") do
test_it(BadContext) do |context, value|
context.public_send("#{value}=", value)
end
end
x.report("better context") do
test_it(BetterContext) do |context, value|
context.public_send("#{value}=", value)
end
end
x.report("hash") do
test_it(Hash) do |context, value|
context[value] = value
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment