From: Eric Hodel Date: 2006-02-15T17:55:09+09:00 Subject: Re: Rescuing blocks? 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