Last active
December 15, 2015 17:49
-
-
Save rklemme/5299235 to your computer and use it in GitHub Desktop.
Modified version which compares the single method check times (which need to include Mock.instance_methods(false)).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
test.report('instance_methods.include?: ') do | |
n.times do | |
Mock.instance_methods(false).include? :k | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ruby respond_to_vs_include-RK.rb | |
user system total real | |
respond_to?: 1.170000 0.000000 1.170000 ( 1.167067) | |
include?: 7.862000 0.000000 7.862000 ( 7.880450) | |
instance_methods.include?: 152.242000 0.000000 152.242000 (152.295711) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment