Skip to content

Instantly share code, notes, and snippets.

@rklemme
Created April 3, 2013 07:45
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 rklemme/5299234 to your computer and use it in GitHub Desktop.
Save rklemme/5299234 to your computer and use it in GitHub Desktop.
# Fancy benchmark...
# is respond_to? so faster than include?
require 'benchmark'
n = 10_000_000
mock_methods = ('a'..'z').to_a
class Mock
end
mock_methods.each do |m|
Mock.class_eval do
send(:attr_accessor, m)
end
end
mock = Mock.new
instance_meths = Mock.instance_methods(false)
Benchmark.bm(15) do |test|
test.report('respond_to?: ') do
n.times do
mock.respond_to? :k
end
end
test.report('include?: ') do
n.times do
instance_meths.include? :k
end
end
end
__END__
-- results: --
user system total real
respond_to?: 2.340000 0.000000 2.340000 ( 2.344671)
include?: 17.180000 0.000000 17.180000 ( 17.192215)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment