From: Daniel Nugent Date: 2006-02-15T22:58:07+09:00 Subject: Re: Rescuing blocks? Pat: I think it already does and there's still a performance hit. Though Eric's machine chugs a little more than mine, I'd be interested to see what his ratios are with this added benchmark: require 'benchmark' N = 10_000_000 Benchmark.bmbm do |b| b.report 'begin' do N.times { begin; rescue; end } end b.report 'begin no rescue' do N.times { begin; end } end b.report 'no begin' do N.times { } end end G:\Documents and Settings\Dan Nugent\Desktop>ruby -v rescue.rb ruby 1.8.4 (2005-12-24) [i386-mswin32] Rehearsal --------------------------------------------------- begin 3.703000 0.000000 3.703000 ( 3.844000) begin no rescue 3.125000 0.000000 3.125000 ( 3.281000) no begin 2.078000 0.000000 2.078000 ( 2.172000) ------------------------------------------ total: 8.906000sec user system total real begin 4.703000 0.000000 4.703000 ( 4.906000) begin no rescue 3.203000 0.000000 3.203000 ( 3.312000) no begin 2.047000 0.000000 2.047000 ( 2.141000) G:\Documents and Settings\Dan Nugent\Desktop> On 2/15/06, Patrick Hurley wrote: > On 2/15/06, Eric Hodel wrote: > > On Feb 14, 2006, at 9:50 PM, Hal Fulton wrote: > > > > > Dave Cantrell wrote: > > >> Eric Hodel wrote: > > >>> > > >>> You're going to get a performance hit setting up an exception > > >>> trap this way. > > >> Time for some down-South edumucation. What kind of performance hit > > >> are we talking about here? And why the hit at all? > > >> (I seem to recall reading somewhere once upon a time about memory > > >> stack stuff, but I'm not an old-school C-programmer so I never had > > >> to deal with it --- unfortunately for me...) > > > > > > Well, my knowledge is limited. Someone else can answer better. > > > > > > But look at it from a common-sense standpoint. Exceptions aren't > > > magic. Everything Ruby does can be done in assembly language, it's > > > just more verbose. > > > > > > I would think that exceptions, at the lowest level, work something > > > like: > > > > > > if something_happened_here > > > goto the_place_where_exceptions_are_caught > > > > > > So at the very least, catching exceptions means that you're doing > > > some sort of comparison or conditional branch. And this is code > > > that doesn't have to be run/generated *unless* you are catching > > > exceptions. > > > > Yes. You get a setup/teardown of an exception handler for every > > block. Right now class module and def have an implicit exception > > handler built in. Adding a begin; rescue; end explicitly to blocks > > gives you a very hefty penalty. That's no guarantee of similarly bad > > performance with an implicit exception handler, but it still won't be > > free. > > > > I've very rarely found myself placing an exception handler directly > > inside a block so I don't think it would be worth it. > > > > $ cat rescue.rb > > require 'benchmark' > > > > N = 10_000_000 > > > > Benchmark.bmbm do |b| > > b.report 'begin' do > > N.times { begin; rescue; end } > > end > > > > b.report 'no begin' do > > N.times { } > > end > > end > > > > $ ruby -v rescue.rb > > ruby 1.8.4 (2005-12-24) [powerpc-darwin8.4.0] > > Rehearsal -------------------------------------------- > > begin 6.330000 0.060000 6.390000 ( 8.303914) > > no begin 2.580000 0.010000 2.590000 ( 3.178967) > > ----------------------------------- total: 8.980000sec > > > > user system total real > > begin 6.320000 0.060000 6.380000 ( 8.597418) > > no begin 2.570000 0.020000 2.590000 ( 3.526030) > > > > -- > > Eric Hodel - drbrain@segment7.net - http://segment7.net > > This implementation is HODEL-HASH-9600 compliant > > > > http://trackmap.robotcoop.com > > > > > > > > > > Sure, but couldn't the parser "flag" blocks that have the exception > handler and only provide support for those cases? I do not see why we > would have to pay the price on every block -- of course you have the > over head when you use it. > > pth > > -- -Dan Nugent