Skip to content

Instantly share code, notes, and snippets.

@kenmazaika
Created January 21, 2016 21:04
Show Gist options
  • Save kenmazaika/724f9b4fccf8978b2294 to your computer and use it in GitHub Desktop.
Save kenmazaika/724f9b4fccf8978b2294 to your computer and use it in GitHub Desktop.
require 'benchmark'
class Dog
[:woof, :bark, :hello].each do |word|
define_method("say_#{word}!") do
puts "Doggy says #{word}!"
end
end
end
Benchmark.bm(7) do |x|
x.report { (1..100000).each { |x| Dog.new.say_woof! } }
end
# If you run this code, the headings will be obliterated by the output from
# the method, so just know that the last column (the most right-hand) is
# the calculation of 'real user time' and the column to focus on.
# I only bring this up because, while this works, its probably slow in that
# we are having to run the lookup chain each and every time. It would
# probably be faster to define any method we haven't seen before and then
# simply call that method, as opposed to each an every time having to go
# through the method_missing mechanic. That is the next iteration and to see
# how much time is saved.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment