Skip to content

Instantly share code, notes, and snippets.

@takkanm
Created September 27, 2011 02:18
Show Gist options
  • Select an option

  • Save takkanm/1244147 to your computer and use it in GitHub Desktop.

Select an option

Save takkanm/1244147 to your computer and use it in GitHub Desktop.
% cat tmp/define_method_test.rb
class Foo
def foo
:foo
end
define_method(:bar) { :foo }
end
def time(method_name, count, &block)
start = Time.now
count.times &block
stop = Time.now
puts "#{method_name}[#{count}] : #{stop - start}"
end
foo = Foo.new
[1, 10, 100, 1000, 10000, 100000].each do |i|
time("foo", i) { foo.foo }
time("bar", i) { foo.bar }
end
% ruby -v tmp/define_method_test.rb
ruby 1.9.2p290 (2011-07-09) [x86_64-darwin11.1.0]
foo[1] : 1.9e-05
bar[1] : 1.1e-05
foo[10] : 3.0e-06
bar[10] : 4.0e-06
foo[100] : 1.6e-05
bar[100] : 2.5e-05
foo[1000] : 0.00015
bar[1000] : 0.000246
foo[10000] : 0.001503
bar[10000] : 0.00245
foo[100000] : 0.015126
bar[100000] : 0.030158
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment