Skip to content

Instantly share code, notes, and snippets.

@cyx
Created February 4, 2013 23:04
Show Gist options
  • Save cyx/4710593 to your computer and use it in GitHub Desktop.
Save cyx/4710593 to your computer and use it in GitHub Desktop.
require "benchmark"
class A
def initialize(atts)
atts.each do |k,v|
instance_variable_set(:"@#{k}", v)
end
end
end
class B
attr_accessor :foo, :bar, :baz
def initialize(atts)
atts.each do |k,v|
send(:"#{k}=", v)
end
end
end
class C
def initialize(atts)
@foo = atts[:foo]
# @bar = atts[:bar]
# @baz = atts[:baz]
end
end
Benchmark.bmbm do |x|
x.report "iv" do
1000.times do
# A.new(foo: "foo", bar: "bar", baz: "baz")
A.new(foo: "foo")
end
end
x.report "send" do
1000.times do
# B.new(foo: "foo", bar: "bar", baz: "baz")
B.new(foo: "foo")
end
end
x.report "straight" do
1000.times do
# C.new(foo: "foo", bar: "bar", baz: "baz")
C.new(foo: "foo")
end
end
end
Rehearsal --------------------------------------------
iv 0.000000 0.000000 0.000000 ( 0.002298)
send 0.000000 0.000000 0.000000 ( 0.001946)
straight 0.000000 0.000000 0.000000 ( 0.001161)
----------------------------------- total: 0.000000sec
user system total real
iv 0.000000 0.000000 0.000000 ( 0.001688)
send 0.000000 0.000000 0.000000 ( 0.001677)
straight 0.000000 0.000000 0.000000 ( 0.000776)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment