# Get CPU usage

**URL:** <https://rubytalk.org/t/get-cpu-usage/50705>\
**Category:** ruby-talk\
**Created:** [14 December 2008 13:01 UTC](https://rubytalk.org/t/get-cpu-usage/50705 "2008-12-14T13:01:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Panda\_Beer](https://avatars.discourse-cdn.com/v4/letter/p/9de0a6/32.png) [@Panda\_Beer](https://rubytalk.org/u/Panda_Beer)\
**Post date:** [14 December 2008 13:01 UTC](https://rubytalk.org/t/get-cpu-usage/50705/1 "2008-12-14T13:01:05Z")

</div>

Hello everybody,

I would like to get the % of CPU usage of a current Ruby script.  
For example, I would like to start my script like and allow it to get  
and print this such as:

./application.rb  
=\> 12:10 =\> CPU usage is 3.2%  
=\> 12:15 =\> CPU usage is 3.1%  
=\> 12:20 =\> CPU usage is 3.1%  
=\> 12:25 =\> CPU usage is 3.5%

Have you got an idea? Many thanks.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [14 December 2008 14:17 UTC](https://rubytalk.org/t/get-cpu-usage/50705/2 "2008-12-14T14:17:50Z")

</div>

$ ruby -e '10.times { 100000.times { 1 + 2 }; p Process.times }'  
#\<struct Struct::Tms utime=0.062, stime=0.046, cutime=0.0, cstime=0.0\>  
#\<struct Struct::Tms utime=0.109, stime=0.046, cutime=0.0, cstime=0.0\>  
#\<struct Struct::Tms utime=0.156, stime=0.046, cutime=0.0, cstime=0.0\>  
#\<struct Struct::Tms utime=0.203, stime=0.046, cutime=0.0, cstime=0.0\>  
#\<struct Struct::Tms utime=0.25, stime=0.046, cutime=0.0, cstime=0.0\>  
#\<struct Struct::Tms utime=0.296, stime=0.046, cutime=0.0, cstime=0.0\>  
#\<struct Struct::Tms utime=0.343, stime=0.046, cutime=0.0, cstime=0.0\>  
#\<struct Struct::Tms utime=0.406, stime=0.046, cutime=0.0, cstime=0.0\>  
#\<struct Struct::Tms utime=0.453, stime=0.046, cutime=0.0, cstime=0.0\>  
#\<struct Struct::Tms utime=0.5, stime=0.046, cutime=0.0, cstime=0.0\>

You could correlate that to real time that has passed.

$ ruby -e 't=Time.now; 10.times { sleep 2; pt=Process.times;p((pt.utime+pt.stime)/(Time.now-t)) }'  
0.015  
0.00749812546863284  
0.00499916680553241  
0.00374953130858643  
0.002999700029997  
0.00249979168402633  
0.00214270409256482  
0.00187488281982376  
0.00166657407921782  
0.00149992500374981

robert@fussel ~  
$ ruby -e 't=Time.now; 10.times {10\_000.times {1+2}; pt=Process.times;p((pt.utime+pt.stime)/(Time.now-t)) }'  
19.25  
8.55555555555556  
5.13333333333333  
4.89473684210526  
3.875  
3.20689655172414  
3.20588235294118  
2.86842105263158  
2.5952380952381  
2.31914893617021

Obviously there is a factor missing...

robert@fussel ~

&nbsp;&nbsp;robert

> **···**
>
> On 14.12.2008 14:01, Panda Beer wrote:
> 
> > Hello everybody,
> > 
> > I would like to get the % of CPU usage of a current Ruby script.  
> > For example, I would like to start my script like and allow it to get  
> > and print this such as:
> > 
> > /application.rb  
> > =\> 12:10 =\> CPU usage is 3.2%  
> > =\> 12:15 =\> CPU usage is 3.1%  
> > =\> 12:20 =\> CPU usage is 3.1%  
> > =\> 12:25 =\> CPU usage is 3.5%
> > 
> > Have you got an idea? Many thanks.
