How do you determine CPU time in a Ruby program?

Thanks to all who helped me understand Ruby’s treatment of
arrays. I was able to successfully finish the 8-puzzle program
afternoon. I’m been putting some code in to measure the time it takes
run the program this morning. Since this is my first “big” program in
I am too embarrassed to distribute copies as I am sure there are better
to write the code.

Ruby, as you all know, is not as fast as compiled code, like C++ or
It takes about 15 minutes for my program to execute. Most of that time
appears to come from the searching for matches in the two queues, a
Queue, for nodes that may or may not be part of the path to the goal,
and a
regular queue (not the Queue of the Thread class). I ran an insolvable
starting/goal combination all last night and this morning there were
entries in the Closed queue and 731+ entries in the Open queue and
near a termination. (It would terminate if all combinations of the 3x3
{9 factorial combinations} were examined and the Open queue had been
to zero entries.) If I have time I’ll return to the code and see if I
rewrite it to execute faster.

Anyway, as I was putting the one line of code to display the time, I
realized that it was the system clock time that was being displayed. I
would like to also display the cpu time. Is it possible to display the
time? By that, I mean the amount of CPU time consumed between a
and ending point in the code. As anyone who has used procexp.exe on a
Windows environment knows, the CPU time must be less than the Clock
time, as
other asynchronous are getting their slice of the time pie, although I
Ruby running at 50% to 95% of the CPU time. The code to record CPU time
will probably unique for a given OS, if it exists. I run on Windows XP
Ruby 1.8. Any ideas how this could be done? It is not documented in
any of
the books I have.

No Sam

I think you’re looking for the benchmark library (in Ruby’s stdlib). To
quote from the docs:
This report shows the user CPU time, system CPU time, the sum of
the user and system CPU times, and the elapsed real time. The unit
of time is seconds.
require ‘benchmark’

     n = 50000 do |x|"for:")   { for i in 1..n; a = "1"; end }"times:") { n.times do   ; a = "1"; end }"upto:")  { 1.upto(n) do ; a = "1"; end }

 The result:

                     user     system      total        real
        for:     1.050000   0.000000   1.050000 (  0.503462)
        times:   1.533333   0.016667   1.550000 (  0.735473)
        upto:    1.500000   0.016667   1.516667 (  0.711239)


On 09/29/2009 05:07 PM, Marvin Gülker wrote: do |x|
        upto:    1.500000   0.016667   1.516667 (  0.711239)


There is also Benchmark.times (process times so far) and
Benchmark.measure (timing of a particular operation):

irb(main):019:0> Benchmark.times
=> #

irb(main):020:0> Benchmark.measure { 10.times do end }
=> #<Benchmark::Tms:0x91f53c0 @label="", @real=1.02519989013672e-05,
@cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>
irb(main):021:0> Benchmark.measure “foo” { 10.times do end }
=> #<Benchmark::Tms:0x91e5650 @label=“foo”, @real=1.00135803222656e-05,
@cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>

Kind regards


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs