How do you determine CPU time in a Ruby program?

M

Mason Kelsey

[Note: parts of this message were removed to make it a legal post.]

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

Ruby, as you all know, is not as fast as compiled code, like C++ or such.
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 Priority
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 1301+
entries in the Closed queue and 731+ entries in the Open queue and nowhere
near a termination. (It would terminate if all combinations of the 3x3 grid
{9 factorial combinations} were examined and the Open queue had been reduced
to zero entries.) If I have time I'll return to the code and see if I can
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 CPU
time? By that, I mean the amount of CPU time consumed between a starting
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 see
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 with
Ruby 1.8. Any ideas how this could be done? It is not documented in any of
the books I have.

No Sam
 
M

Marvin Gülker

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
Benchmark.bm(7) do |x|
x.report("for:") { for i in 1..n; a = "1"; end }
x.report("times:") { n.times do ; a = "1"; end }
x.report("upto:") { 1.upto(n) do ; a = "1"; end }
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)

Marvin
 
R

Robert Klemme

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
Benchmark.bm(7) do |x|
x.report("for:") { for i in 1..n; a = "1"; end }
x.report("times:") { n.times do ; a = "1"; end }
x.report("upto:") { 1.upto(n) do ; a = "1"; end }
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)

Marvin

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

irb(main):019:0> Benchmark.times
=> #<struct Struct::Tms utime=0.11, stime=0.06, cutime=0.0, cstime=0.0>

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>
irb(main):022:0>

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top