Created
October 27, 2022 05:59
-
-
Save kenchan/a3492568c9e39ff8a4f46979182a2ace to your computer and use it in GitHub Desktop.
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
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'benchmark-ips' | |
end | |
require 'benchmark/ips' | |
Benchmark.ips do |x| | |
puts "Ruby: #{RUBY_VERSION}" | |
x.report('raise') { | |
begin | |
"a".not_exists_method | |
result = 'exists' | |
rescue | |
result = 'not exists' | |
end | |
} | |
x.report('respond_to') { | |
begin | |
if "a".respond_to? :not_exists_method | |
result = 'exists' | |
else | |
result = 'not exists' | |
end | |
rescue | |
result = 'not exists' | |
end | |
} | |
x.compare! | |
end | |
__END__ | |
Ruby: 3.1.2 | |
Warming up -------------------------------------- | |
raise 88.271k i/100ms | |
respond_to 862.317k i/100ms | |
Calculating ------------------------------------- | |
raise 890.704k (± 4.9%) i/s - 4.502M in 5.066936s | |
respond_to 8.591M (± 4.7%) i/s - 43.116M in 5.030365s | |
Comparison: | |
respond_to: 8591086.5 i/s | |
raise: 890704.1 i/s - 9.65x (± 0.00) slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment