Created
October 6, 2011 18:23
-
-
Save aq1018/1268188 to your computer and use it in GitHub Desktop.
Benchmark on using instance_variable_set v.s. instance_eval
This file contains hidden or 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' | |
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