Rescuing blocks?

On a lark, I did this, and was shocked at the result. Why the massive difference in performance between "begin; end" within a block, and "{ }" within a block?

I may do some more tinkering with this later on.

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 nested' do
     N.times { { } }
   end

   b.report 'plain' do
     N.times { }
   end
end

Rehearsal ---------------------------------------------------
begin 3.547000 0.000000 3.547000 ( 3.656000)
begin no rescue 2.797000 0.000000 2.797000 ( 2.860000)
no begin nested 38.062000 0.453000 38.515000 ( 40.593000)
plain 1.735000 0.000000 1.735000 ( 1.782000)
----------------------------------------- total: 46.594000sec

                       user system total real
begin 3.516000 0.000000 3.516000 ( 3.594000)
begin no rescue 2.750000 0.000000 2.750000 ( 2.828000)
no begin nested 36.422000 0.297000 36.719000 ( 38.797000)
plain 1.750000 0.000000 1.750000 ( 1.781000)

+1

···

On 2/12/06, David Vallner <david@vallner.net> wrote:

Dňa Nedeľa 12 Február 2006 22:46 Daniel Nugent napísal:
> Sorry, not sure what the issue is (I assume you mean rescue, not raise
> there).
>
> If a rescue appears after a statement, it only applies to that
> statement. If a rescue appears as the first token on a line, it
> applies to the enclosing block. I'm pretty sure that's how it works
> now.

<dense>D'oh. Didn't know that one... </dense>

David Vallner

This is equivalent to N.times { Hash.new }

···

On Feb 15, 2006, at 2:03 PM, Dave Cantrell wrote:

N.times { { } }

Dňa Streda 15 Február 2006 20:03 Dave Cantrell napísal:

On a lark, I did this, and was shocked at the result. Why the massive
difference in performance between "begin; end" within a block, and "{ }"
within a block?

Because it's a literal hash constructor, not a nested block?

On a slightly related note, the literal hash constructor is apparently faster
than Hash.new. Good to know.

David Vallner

So, maybe this calls for a RCR?

I know for sure that I'd like blocks to be rescuable.

···

On 2/13/06, Patrick Hurley <phurley@gmail.com> wrote:

On 2/12/06, David Vallner <david@vallner.net> wrote:
>
>
> Dňa Nedeľa 12 Február 2006 22:46 Daniel Nugent napísal:
> > Sorry, not sure what the issue is (I assume you mean rescue, not raise
> > there).
> >
> > If a rescue appears after a statement, it only applies to that
> > statement. If a rescue appears as the first token on a line, it
> > applies to the enclosing block. I'm pretty sure that's how it works
> > now.
>
> <dense>D'oh. Didn't know that one... </dense>
>
>
> David Vallner
>
>

+1

--
-Dan Nugent

Dňa Pondelok 13 Február 2006 17:20 Patrick Hurley napísal:

+1

Die.

David Vallner
Allergic to anything remotely resembling slashdot