Skip to content

Instantly share code, notes, and snippets.

@Hettomei
Last active August 29, 2015 14:17
Show Gist options
  • Save Hettomei/b2c6ac05f3817b3547b3 to your computer and use it in GitHub Desktop.
Save Hettomei/b2c6ac05f3817b3547b3 to your computer and use it in GitHub Desktop.
bench respond to vs rescue NoMethod
#Rehearsal ---------------------------------------
#old 4.600000 0.060000 4.660000 ( 4.653225)
#new 0.280000 0.000000 0.280000 ( 0.283112)
#------------------------------ total: 4.940000sec
#
# user system total real
#old 4.620000 0.070000 4.690000 ( 4.688474)
#new 0.280000 0.000000 0.280000 ( 0.283607)
require 'benchmark'
iteration = 1000000
def test_rescue
begin
disabled_method?
rescue NoMethodError
true
end
end
def test_ask
respond_to?("disabled_method?") ? disabled_method? : true
end
Benchmark.bmbm do |performance|
performance.report("old") do
iteration.times do
test_rescue
end
end
performance.report("new") do
iteration.times do
test_ask
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment