# Better profiling for ruby?

**URL:** <https://www.ruby-forum.com/t/better-profiling-for-ruby/100352>\
**Category:** Ruby\
**Created:** [July 28, 2006, 3:48pm UTC](https://www.ruby-forum.com/t/better-profiling-for-ruby/100352 "2006-07-28T15:48:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [July 28, 2006, 3:48pm UTC](https://www.ruby-forum.com/t/better-profiling-for-ruby/100352/1 "2006-07-28T15:48:20Z")

</div>

So the recent performance threads have all repeated the wisdom  
“profile first” in various forms, and various ways.

I have a problem with this. I can’t read the output of the ruby  
profiler and get meaningful insights on my script.

Invariably, the profiler will tell me that almost all of my time is  
being sucked up in Range#each or Integer#upto. In other words, the  
profiler is telling me that my time is being sucked up into some loop  
somewhere. Thanks a bunch; any idea which of the twenty loops in my  
program that is?

Over in the perl world, my favorite profiler has been  
Devel::SmallProf. I’ve found there’s nothing to compare with it for  
producing those surprising “so _thats_ where all my time has been  
going” moments. It does this by giving me an easy feedback on which  
lines of my program suck up the most time (both in terms of that line  
directly and, if the line contains a sub call, the lines it calls).  
I’ve found this line-number-based approach to profiling incredibly  
helpful, much more helpful than traditional method-based summaries.

In fact, I found this approach so useful that I even implemented it  
for java ([http://perlmonks.org/?node\_id=381641](http://perlmonks.org/?node_id=381641)). This has been a real  
blessing in my current job, allowing me to focus performance tuning at  
a level that seems natural.

Now, is there any way to get this kind of information for ruby? I’d  
like to be able to look at the output of a profiler and get some  
information useful to making the script faster out of it. Barring  
that, is there somewhere on the web a worked example of taking a  
initial (nontrivial) ruby script, working with the profiler, and  
ending up with a faster script?

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [July 28, 2006, 3:55pm UTC](https://www.ruby-forum.com/t/better-profiling-for-ruby/100352/2 "2006-07-28T15:55:20Z")

</div>

I don’t know whether this article fits into ‘trivial’ category or not:

[http://www-128.ibm.com/developerworks/edu/os-dw-os-ruby2-i.html](http://www-128.ibm.com/developerworks/edu/os-dw-os-ruby2-i.html)  
(registration needed)

J.

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [July 28, 2006, 4:22pm UTC](https://www.ruby-forum.com/t/better-profiling-for-ruby/100352/3 "2006-07-28T16:22:10Z")

</div>

On 7/28/06, Jan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I don’t know whether this article fits into ‘trivial’ category or not:

It almost certainly does. I wrote it as an introduction profiling and  
benchmarking for beginning Ruby programmers. I (or someone)  
should probably write a more advanced example. (It would be  
awesome to look over Zed’s shoulder while he works on mongrel –  
hint, hint.)

> [http://www-128.ibm.com/developerworks/edu/os-dw-os-ruby2-i.html](http://www-128.ibm.com/developerworks/edu/os-dw-os-ruby2-i.html)  
> (registration needed)

Thanks for the link though.

On an unrelated, but important, note – try not to top post. It  
makes your reader work harder to get the context for your  
comments and/or questions.

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [July 28, 2006, 7:14pm UTC](https://www.ruby-forum.com/t/better-profiling-for-ruby/100352/4 "2006-07-28T19:14:21Z")

</div>

Daniel M. wrote:

> program that is?  
> In fact, I found this approach so useful that I even implemented it  
> for java ([http://perlmonks.org/?node\_id=381641](http://perlmonks.org/?node_id=381641)). This has been a real  
> blessing in my current job, allowing me to focus performance tuning at  
> a level that seems natural.
> 
> Now, is there any way to get this kind of information for ruby? I’d  
> like to be able to look at the output of a profiler and get some  
> information useful to making the script faster out of it. Barring  
> that, is there somewhere on the web a worked example of taking a  
> initial (nontrivial) ruby script, working with the profiler, and  
> ending up with a faster script

ruby-prof is pretty neat. I haven’t used it a lot, but I feel it’s a bit  
more informative (and much faster) than the standard Ruby profiler.

[http://rubyforge.org/projects/ruby-prof/](http://rubyforge.org/projects/ruby-prof/)

-Justin

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [July 30, 2006, 8:34pm UTC](https://www.ruby-forum.com/t/better-profiling-for-ruby/100352/5 "2006-07-30T20:34:50Z")

</div>

One thing I forgot - I wrote up a bit more information about ruby-prof  
on my blog at:

> **[ruby-prof 0.4.0 with call graphs | CFIS](https://cfis.savagexi.com/2006/06/21/ruby-prof-0-4-0-with-call-graphs/)**
>
> Update: ruby-prof 0.5.0 is now available and has significantly more features than 0.4.0, including better threading support, rails support, call tree output, etc. After porting ruby-prof to Windows a couple of weeks ago, Shugo Maeda, ruby-prof's...

Hope this helps,

Charlie

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [July 30, 2006, 8:28pm UTC](https://www.ruby-forum.com/t/better-profiling-for-ruby/100352/6 "2006-07-30T20:28:38Z")

</div>

Daniel,

Take a look at ruby-prof, [http://ruby-prof.rubyforge.org/](http://ruby-prof.rubyforge.org/), which Shugo  
and I put together.

The graph profile output will show you what you want. Here is an  
example:

[http://ruby-prof.rubyforge.org/graph.html](http://ruby-prof.rubyforge.org/graph.html)

Thanks,

Charlie

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [August 1, 2006, 11:02am UTC](https://www.ruby-forum.com/t/better-profiling-for-ruby/100352/7 "2006-08-01T11:02:43Z")

</div>

In message [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid), Daniel M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes

> Now, is there any way to get this kind of information for ruby? I’d

Ruby Performance Validator  
[Software Tools | Software Verify](http://www.softwareverify.com/ruby/profiler/index.html)

Stephen

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [July 31, 2006, 12:51pm UTC](https://www.ruby-forum.com/t/better-profiling-for-ruby/100352/8 "2006-07-31T12:51:03Z")

</div>

Charlie S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> The graph profile output will show you what you want. Here is an example:
> 
> [http://ruby-prof.rubyforge.org/graph.html](http://ruby-prof.rubyforge.org/graph.html)

While I have no doubt that it shows me something, I had no idea what  
that might be until I tracked down prime.rb in the source - if this is  
supposed to be readable without access to the code being tested, I  
don’t understand. (And if it isn’t supposed to be readable without  
looking at the test source, why is it so hard to find that source  
file? I shouldn’t have to download the distribution to be able to  
read the documentation.)

Now that I’ve seen the source, and gone over what it says in  
graph.txt, I have a slightly better idea. This might indeed be able  
to tell me something; I’ll have to try it thw next time I’m trying to  
optimize ruby.

It’d still be nice to have something like perl’s Devel::SmallProf, but  
I think I can live without that for a bit.

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [August 2, 2006, 12:46am UTC](https://www.ruby-forum.com/t/better-profiling-for-ruby/100352/9 "2006-08-02T00:46:33Z")

</div>

Hi Daniel,

Good point on making prime.rb available on line - here it is:

[http://cfis.savagexi.com/pages/prime.rb](http://cfis.savagexi.com/pages/prime.rb)

I also updated my blog entry.

Anyway, graph profiles definitely take some getting used to. But they  
answer the question you had - given a particular hotspot in the code how  
does that code get executed (what methods call it, what methods does it  
call).

I wrote up some documentation via Rdoc, but another great source of  
information is doing a search for GProf tutorials.

As far as SmallProf, I’ve never used it. What do you like about it?

Thanks,

Charlie
