From: Robert Klemme Date: 2005-09-14T21:56:34+09:00 Subject: Re: Optimizing a single slow method ------=_NextPart_000_0118_01C5B93C.36D7B610 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Hugh Sasse wrote: > On Wed, 14 Sep 2005, Robert Klemme wrote: > >> Hugh Sasse wrote: >>>> see an associated block, it doesn't handle the cleanup for you. >>> >>> Yes, that's right. I wonder if it is worth the price in garbage >>> collection to explicitly close in this case? Probably not given >>> maintenance costs. >> >> The closing isn't that costly. > > The closing would allow you to avoid that block with the |io| in it. > That cost, the cost of the object, is what I meant. What object exactly do you mean? The IO instance is there anyway so you probably are referring to the overhead incurred by the block. I don't think that this is significant. 14:50:33 [ruby]: ruby block-overhead.rb 1000000 user system total real block 3.563000 0.016000 3.579000 ( 3.569000) no block 1.859000 0.000000 1.859000 ( 1.867000) That's 1.704 seconds so 1.7 micro seconds per invocation. Not really much compared to the overhead of opening the file and all the other work. Kind regards robert ------=_NextPart_000_0118_01C5B93C.36D7B610 Content-Type: application/octet-stream; name="block-overhead.rb" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="block-overhead.rb" =0A= require 'benchmark'=0A= =0A= REP =3D 1_000_000=0A= =0A= p REP=0A= =0A= def foo=0A= if block_given?=0A= yield 0=0A= else=0A= 0=0A= end=0A= end=0A= =0A= =0A= Benchmark.bm(20) do |bb|=0A= bb.report "block" do=0A= REP.times { foo() {|i| i + 1 } }=0A= end=0A= =0A= bb.report "no block" do=0A= REP.times { foo() + 1 }=0A= end=0A= end=0A= ------=_NextPart_000_0118_01C5B93C.36D7B610--