# How do you determine CPU time in a Ruby program?

**URL:** <https://www.ruby-forum.com/t/how-do-you-determine-cpu-time-in-a-ruby-program/176362>\
**Category:** Ruby\
**Created:** [September 29, 2009, 4:45pm UTC](https://www.ruby-forum.com/t/how-do-you-determine-cpu-time-in-a-ruby-program/176362 "2009-09-29T16:45:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [September 29, 2009, 4:45pm UTC](https://www.ruby-forum.com/t/how-do-you-determine-cpu-time-in-a-ruby-program/176362/1 "2009-09-29T16:45:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [September 29, 2009, 5:07pm UTC](https://www.ruby-forum.com/t/how-do-you-determine-cpu-time-in-a-ruby-program/176362/2 "2009-09-29T17:07:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [September 30, 2009, 8:30am UTC](https://www.ruby-forum.com/t/how-do-you-determine-cpu-time-in-a-ruby-program/176362/3 "2009-09-30T08:30:54Z")

</div>

On 09/29/2009 05:07 PM, Marvin Gülker wrote:

> ```
> Benchmark.bm(7) do |x|
> 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  
=\> #

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
