Skip to content

Instantly share code, notes, and snippets.

@apotonick
Created June 2, 2015 11:52
Show Gist options
  • Save apotonick/1353693f41f31dbd21e6 to your computer and use it in GitHub Desktop.
Save apotonick/1353693f41f31dbd21e6 to your computer and use it in GitHub Desktop.
gem "ruby-prof"
require "ruby-prof"
Song = Struct.new(:a, :b, :c, :d, :e, :f, :g, :h, :i, :j, :k, :l, :m, :n, :o) # FIXME: what if we don't have STruct#options
song = Song.new(1,2,3,4,5,6,7,8,9,10)
class CopyTwin
def self.attrs
[:a, :b, :c, :d, :e, :f, :g, :h, :i, :j]
end
def initialize(model, options)
@fields = {}
self.class.attrs.each do |meth|
@fields[meth] = model.send(meth)
end
options.each do |k,v|
@fields[k] = v
end
end
[:a, :b, :c, :d, :e, :f, :g, :h, :i, :j, :k, :l, :m, :n, :o].each do |meth|
define_method meth do
@fields[meth]
end
end
end
RubyProf.start
10000.times do
twin = CopyTwin.new(song, k: 11, l: 12, m: 15, n: 16, o: 17)
twin.a
twin.b
twin.c
twin.d
twin.e
twin.f
twin.g
twin.h
twin.i
twin.j
end
res = RubyProf.stop
printer = RubyProf::FlatPrinter.new(res)
puts "copy:"
printer.print(STDOUT)
class AttrTwin
def initialize(model, options)
[:a, :b, :c, :d, :e, :f, :g, :h, :i, :j].each do |meth|
instance_variable_set("@#{meth}", model.send(meth))
end
options.each do |k,v|
instance_variable_set("@#{k}", v)
end
end
[:a, :b, :c, :d, :e, :f, :g, :h, :i, :j, :k, :l, :m, :n, :o].each do |meth|
attr_reader meth
end
end
RubyProf.start
10000.times do
twin = AttrTwin.new(song, k: 11, l: 12, m: 15, n: 16, o: 17)
twin.a
twin.b
twin.c
twin.d
twin.e
twin.f
twin.g
twin.h
twin.i
twin.j
end
res = RubyProf.stop
printer = RubyProf::FlatPrinter.new(res)
puts "attr_reader:"
printer.print(STDOUT)
require "forwardable"
class ForwardableTwin
extend Forwardable
def initialize(model, options)
# [:a, :b, :c, :d, :e, :f, :g, :h, :i, :j].each do |meth|
# instance_variable_set("@#{meth}", model.send(meth))
# end
@model = model
@fields = {}
options.each do |k,v|
@fields[k] = v
end
end
[:a, :b, :c, :d, :e, :f, :g, :h, :i, :j, :k, :l, :m, :n, :o].each do |meth|
def_delegator :@model, meth
end
end
RubyProf.start
10000.times do
twin = ForwardableTwin.new(song, k: 11, l: 12, m: 15, n: 16, o: 17)
twin.a
twin.b
twin.c
twin.d
twin.e
twin.f
twin.g
twin.h
twin.i
twin.j
end
res = RubyProf.stop
printer = RubyProf::FlatPrinter.new(res)
puts "forwardable:"
printer.print(STDOUT)
class DelegateTwin
def initialize(model, options)
# [:a, :b, :c, :d, :e, :f, :g, :h, :i, :j].each do |meth|
# instance_variable_set("@#{meth}", model.send(meth))
# end
@model = model
@fields = {}
options.each do |k,v|
@fields[k] = v
end
end
[:a, :b, :c, :d, :e, :f, :g, :h, :i, :j, :k, :l, :m, :n, :o].each do |meth|
define_method meth do
@model.send(meth)
end
end
end
RubyProf.start
10000.times do
twin = DelegateTwin.new(song, k: 11, l: 12, m: 15, n: 16, o: 17)
twin.a
twin.b
twin.c
twin.d
twin.e
twin.f
twin.g
twin.h
twin.i
twin.j
end
res = RubyProf.stop
printer = RubyProf::FlatPrinter.new(res)
puts "delegate:"
printer.print(STDOUT)
gem "disposable", path: "../disposable"
require "disposable/twin"
class SongTwin < Disposable::Twin
[:a, :b, :c, :d, :e, :f, :g, :h, :i, :j, :k, :l, :m, :n, :o].each do |meth|
property meth
end
end
# SongTwin.new(song)
RubyProf.start
10000.times do
twin = SongTwin.new(song, k: 11, l: 12, m: 15, n: 16, o: 17)
twin.a
twin.b
twin.c
twin.d
twin.e
twin.f
twin.g
twin.h
twin.i
twin.j
end
res = RubyProf.stop
printer = RubyProf::FlatPrinter.new(res)
puts "twin, no setup:"
printer.print(STDOUT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment