Skip to content

@aq1018 /bench.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.