[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Elapsed time -- tiny code snippet

Hal E. Fulton

4/13/2005 12:16:00 AM

Here's a poor man's profiler -- only good for finding the
time in seconds it took to execute a code block. (I used
this today in a context where I was doing several operations
that invoked external programs and such.)

I put it inside Time just for convenience.


Cheers,
Hal


class Time
def self.elapse
raise "Need block" unless block_given?
t0 = Time.now
yield
Time.now - t0
end
end

secs = Time.elapse { do_something() }




1 Answer

Florian Groß

4/13/2005 12:19:00 AM

0

Hal Fulton wrote:

> Here's a poor man's profiler -- only good for finding the
> time in seconds it took to execute a code block. (I used
> this today in a context where I was doing several operations
> that invoked external programs and such.)
>
> I put it inside Time just for convenience.
>
> class Time
> def self.elapse
> raise "Need block" unless block_given?
> t0 = Time.now
> yield
> Time.now - t0
> end
> end
>
> secs = Time.elapse { do_something() }

I'd suggest converting the times to floats (via .to_f) before
subtracting -- it ought to be quite a bit more precise that way.