Created
March 6, 2016 13:05
-
-
Save gfmurphy/1bfa1b34ec356a294f5b to your computer and use it in GitHub Desktop.
OpenStruct Performance Test
This file contains 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 "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