From: Phrogz Date: 2006-12-30T08:55:05+09:00 Subject: Re: Methods validating their arguments: good or bad? Phrogz wrote: > When the debug flag is supplied, the block is yielded and run. When > not, the entry_check call still does have the overhead of a do-nothing > method call, but far less than before. Some followup performance tests from this idea: def check; yield; end def nothing; end def no_test( str ) str[ 1..3 ] end def test_inline( str ) raise "OUCH" unless str.is_a?( String ) && str.length > 3 str[ 1..3 ] end def test_through_block( str ) check{ raise "OUCH" unless str.is_a?( String ) && str.length > 3 } str[ 1..3 ] end def dead_method_call( str ) nothing{ raise "OUCH" unless str.is_a?( String ) && str.length > 3 } str[ 1..3 ] end require 'benchmark' s = "Hello" N = 1_000_000 Benchmark.bmbm{ |x| x.report( 'no_test' ){ N.times{ no_test( s ) } } x.report( 'dead_method_call' ){ N.times{ dead_method_call( s ) } } x.report( 'test_inline' ){ N.times{ test_inline( s ) } } x.report( 'test_through_block' ){ N.times{ test_through_block( s ) } } } __END__ Rehearsal ------------------------------------------------------ no_test 2.930000 0.020000 2.950000 ( 2.999768) dead_method_call 4.030000 0.050000 4.080000 ( 4.232545) test_inline 4.830000 0.050000 4.880000 ( 5.065821) test_through_block 6.100000 0.050000 6.150000 ( 6.227374) -------------------------------------------- total: 18.060000sec user system total real no_test 2.920000 0.020000 2.940000 ( 2.971835) dead_method_call 3.910000 0.030000 3.940000 ( 3.990496) test_inline 4.750000 0.040000 4.790000 ( 4.825439) test_through_block 6.140000 0.050000 6.190000 ( 6.263266)