From: Eric Hodel Date: 2005-06-03T07:58:40+09:00 Subject: Re: Interesting discovery... On 02 Jun 2005, at 12:43, Joel VanderWerf wrote: > Ara.T.Howard@noaa.gov wrote: > >> Benchmark::bm do |b| >> %w(rescue_test respond_to_test).map{|m| b.report(m){c.send m,n}} >> end >> harp:~ > ruby a.rb >> user system total real >> rescue_test 2.940000 0.320000 3.260000 ( 3.264758) >> respond_to_test 0.160000 0.050000 0.210000 ( 0.208268) >> looks like respond_to? is indeed faster. >> > > But if the method *is* defined, rescue is faster: > > class C > def a; end # <== added > def c; end > > So, if some obejcts have the methods and some dont, the best choice > depends on the balance between the two branches (defined vs. > undefined). class C def a; Object.no_such_method; end end rescue NoMethodError is an inappropriate test for whether or not a method exists. (This may or may not be important depending on what what else you need to rescue.) Being faster does not make it the correct solution. -- Eric Hodel - drbrain@segment7.net - http://segment7.net FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04