[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

display computation time in IRB

Jason

3/20/2009 4:19:00 PM

Is there an easy way to display the time it takes to compute any
function from within IRB?
--
Posted via http://www.ruby-....

4 Answers

david wright

3/20/2009 10:16:00 PM

0

Jason Lillywhite wrote:
> Is there an easy way to display the time it takes to compute any
> function from within IRB?


If I understand correctly, there is the Benchmark module

http://www.ruby-doc.org/stdlib/libdoc/benchmark/rdoc/classes/Benc...


>> require 'benchmark'
=> true
>> Benchmark.bmbm do |x|
?> x.report('concat') {s='t ';(1..10000).step do |x| s.concat(x.to_s)
end}
?> x.report('<<') {s='t ';(1..10000).step do |x| s << x.to_s end}
>> end
Rehearsal ------------------------------------------
concat 0.010000 0.000000 0.010000 ( 0.008603)
<< 0.010000 0.000000 0.010000 ( 0.007354)
--------------------------------- total: 0.020000sec

user system total real
concat 0.000000 0.000000 0.000000 ( 0.006930)
<< 0.010000 0.000000 0.010000 ( 0.007101)
--
Posted via http://www.ruby-....

Jason

3/20/2009 10:42:00 PM

0

yes, that is what I wanted. I just didn't know how to ask for it.

Thanks.
--
Posted via http://www.ruby-....

Chris Shea

3/20/2009 10:49:00 PM

0

On Mar 20, 10:18 am, Jason Lillywhite <jason.lillywh...@gmail.com>
wrote:
> Is there an easy way to display the time it takes to compute any
> function from within IRB?
> --
> Posted viahttp://www.ruby-....

It's not unheard of for people to add something like this to their
irbrc:

def time
start = Time.now
result = yield
puts "Completed in #{Time.now - start} seconds."
result
end

In use:

016:0> time { ('a'*10_000_000).sub(/a+/,'hi') }
Completed in 0.446504 seconds.
"hi"

HTH,
Chris

Joel VanderWerf

3/20/2009 10:59:00 PM

0

Chris Shea wrote:
> On Mar 20, 10:18 am, Jason Lillywhite <jason.lillywh...@gmail.com>
> wrote:
>> Is there an easy way to display the time it takes to compute any
>> function from within IRB?
>> --
>> Posted viahttp://www.ruby-....
>
> It's not unheard of for people to add something like this to their
> irbrc:
>
> def time
> start = Time.now
> result = yield
> puts "Completed in #{Time.now - start} seconds."
> result
> end
>
> In use:
>
> 016:0> time { ('a'*10_000_000).sub(/a+/,'hi') }
> Completed in 0.446504 seconds.
> "hi"
>
> HTH,
> Chris
>

Or something like this:

irb(main):005:0> def time
irb(main):006:1> start = Process.times.inject{|s,x|s+x}
irb(main):007:1> result = yield
irb(main):008:1> finish = Process.times.inject{|s,x|s+x}
irb(main):009:1> puts "Completed in #{finish - start} cpu seconds"
irb(main):010:1> result
irb(main):011:1> end
=> nil
irb(main):012:0> time {sleep 1}
Completed in 0.0 cpu seconds
=> 1
irb(main):013:0> time {1000000.times {1.23**42}}
Completed in 0.57 cpu seconds
=> 1000000

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407