Skip to content

Instantly share code, notes, and snippets.

@cyx
Created February 4, 2013 22:57
Show Gist options
  • Select an option

  • Save cyx/4710531 to your computer and use it in GitHub Desktop.

Select an option

Save cyx/4710531 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")
end
end
x.report "send" do
1000.times do
B.new(foo: "foo", bar: "bar", baz: "baz")
end
end
x.report "straight" do
1000.times do
C.new(foo: "foo", bar: "bar", baz: "baz")
end
end
end
Rehearsal --------------------------------------------
iv 0.000000 0.000000 0.000000 ( 0.004341)
send 0.010000 0.000000 0.010000 ( 0.004375)
straight 0.000000 0.000000 0.000000 ( 0.001497)
----------------------------------- total: 0.010000sec
user system total real
iv 0.000000 0.000000 0.000000 ( 0.003495)
send 0.010000 0.000000 0.010000 ( 0.003770)
straight 0.000000 0.000000 0.000000 ( 0.001198)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment