Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require 'benchmark'
n = 100_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)
mock_methods.map! { |e| e.to_sym }
Benchmark.bmbm(15) do |test|
mock_methods.each do |meth|
test.report("respond_to?(#{meth})") do
n.times do
mock.respond_to? meth
end
end
end
mock_methods.each do |meth|
test.report("include?(#{meth})") do
n.times do
instance_meths.include? meth
end
end
end
end
__END__
user system total real
respond_to?(a) 0.030000 0.000000 0.030000 ( 0.031189)
respond_to?(b) 0.030000 0.000000 0.030000 ( 0.031101)
respond_to?(c) 0.030000 0.000000 0.030000 ( 0.031507)
respond_to?(d) 0.030000 0.000000 0.030000 ( 0.030424)
respond_to?(e) 0.040000 0.000000 0.040000 ( 0.030514)
respond_to?(f) 0.030000 0.000000 0.030000 ( 0.030814)
respond_to?(g) 0.030000 0.000000 0.030000 ( 0.030453)
respond_to?(h) 0.030000 0.000000 0.030000 ( 0.030743)
respond_to?(i) 0.030000 0.000000 0.030000 ( 0.030704)
respond_to?(j) 0.030000 0.000000 0.030000 ( 0.030873)
respond_to?(k) 0.030000 0.000000 0.030000 ( 0.030319)
respond_to?(l) 0.030000 0.000000 0.030000 ( 0.030522)
respond_to?(m) 0.030000 0.000000 0.030000 ( 0.030689)
respond_to?(n) 0.030000 0.000000 0.030000 ( 0.030612)
respond_to?(o) 0.030000 0.000000 0.030000 ( 0.029768)
respond_to?(p) 0.030000 0.000000 0.030000 ( 0.029741)
respond_to?(q) 0.030000 0.000000 0.030000 ( 0.029653)
respond_to?(r) 0.030000 0.000000 0.030000 ( 0.029917)
respond_to?(s) 0.030000 0.000000 0.030000 ( 0.029884)
respond_to?(t) 0.030000 0.000000 0.030000 ( 0.029679)
respond_to?(u) 0.030000 0.000000 0.030000 ( 0.029821)
respond_to?(v) 0.020000 0.000000 0.020000 ( 0.029289)
respond_to?(w) 0.020000 0.000000 0.020000 ( 0.029658)
respond_to?(x) 0.030000 0.000000 0.030000 ( 0.029806)
respond_to?(y) 0.030000 0.000000 0.030000 ( 0.030761)
respond_to?(z) 0.030000 0.000000 0.030000 ( 0.029590)
include?(a) 0.020000 0.000000 0.020000 ( 0.023256)
include?(b) 0.050000 0.000000 0.050000 ( 0.050267)
include?(c) 0.070000 0.000000 0.070000 ( 0.070923)
include?(d) 0.090000 0.000000 0.090000 ( 0.091891)
include?(e) 0.110000 0.000000 0.110000 ( 0.116640)
include?(f) 0.140000 0.000000 0.140000 ( 0.141270)
include?(g) 0.160000 0.000000 0.160000 ( 0.159342)
include?(h) 0.180000 0.000000 0.180000 ( 0.182828)
include?(i) 0.210000 0.000000 0.210000 ( 0.206258)
include?(j) 0.220000 0.000000 0.220000 ( 0.225916)
include?(k) 0.240000 0.000000 0.240000 ( 0.249562)
include?(l) 0.260000 0.000000 0.260000 ( 0.267549)
include?(m) 0.300000 0.000000 0.300000 ( 0.296218)
include?(n) 0.320000 0.000000 0.320000 ( 0.316334)
include?(o) 0.350000 0.000000 0.350000 ( 0.350392)
include?(p) 0.370000 0.000000 0.370000 ( 0.367885)
include?(q) 0.380000 0.000000 0.380000 ( 0.388711)
include?(r) 0.420000 0.000000 0.420000 ( 0.423893)
include?(s) 0.440000 0.000000 0.440000 ( 0.437467)
include?(t) 0.450000 0.000000 0.450000 ( 0.451896)
include?(u) 0.470000 0.000000 0.470000 ( 0.470976)
include?(v) 0.490000 0.000000 0.490000 ( 0.494128)
include?(w) 0.510000 0.000000 0.510000 ( 0.516354)
include?(x) 0.540000 0.000000 0.540000 ( 0.534326)
include?(y) 0.550000 0.000000 0.550000 ( 0.558297)
include?(z) 0.560000 0.000000 0.560000 ( 0.569605)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment