Skip to content

Instantly share code, notes, and snippets.

@aq1018
Created October 6, 2011 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aq1018/1268188 to your computer and use it in GitHub Desktop.
Save aq1018/1268188 to your computer and use it in GitHub Desktop.
Benchmark on using instance_variable_set v.s. instance_eval
require 'benchmark'
class Foo
def bar
true
end
end
n = 1_000_000
Benchmark.bm(30) do | b |
b.report "instance_variable_set" do
n.times do
instance = Foo.new
instance.instance_variable_set(:@baz, Hash.new)
end
end
b.report "instance_eval" do
n.times do
instance = Foo.new
instance.instance_eval do
@baz = Hash.new
end
end
end
end
# user system total real
# instance_variable_set 0.720000 0.000000 0.720000 ( 0.715357)
# instance_eval 5.280000 0.000000 5.280000 ( 5.284949)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment