# Process for optimizing ruby code

**URL:** <https://rubytalk.org/t/process-for-optimizing-ruby-code/60465>\
**Category:** ruby-talk\
**Created:** [15 October 2010 13:20 UTC](https://rubytalk.org/t/process-for-optimizing-ruby-code/60465 "2010-10-15T13:20:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Wagner](https://avatars.discourse-cdn.com/v4/letter/a/22d042/32.png) [@Andrew\_Wagner](https://rubytalk.org/u/Andrew_Wagner)\
**Post date:** [15 October 2010 13:20 UTC](https://rubytalk.org/t/process-for-optimizing-ruby-code/60465/1 "2010-10-15T13:20:33Z")

</div>

So, I've got a chunk of code which needs optimizing. Yes, it really needs  
it, I'm not optimizing prematurely. It (mostly) works, it's just way too  
slow. And I knew it would be too slow when I wrote it, I just wanted to come  
up with a simple implementation first, and get it under test.

That said, I'm wondering if there are any suggestions out there for  
tutorials on using ruby-prof. Not tutorials like "here are what the numbers  
mean", but "here are some suggestions for looking at the numbers and  
deciding where you'll get the most gain in optimizing.

Thanks!

---

<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:** [15 October 2010 14:54 UTC](https://rubytalk.org/t/process-for-optimizing-ruby-code/60465/2 "2010-10-15T14:54:58Z")

</div>

That's the usual approach when tuning: take the slowest part, make it  
faster. Repeat until performance is sufficient - or you run out of  
money.

A generally candidates for optimization is object allocation. That is  
slow compared to other operations because of the GC management  
involved. Also, if you do lookups in huge Array you might want to  
switch to indexed access via Hash.

Kind regards

robert

> **···**
>
> On Fri, Oct 15, 2010 at 3:20 PM, Andrew Wagner \<wagner.andrew@gmail.com\> wrote:
> 
> > So, I've got a chunk of code which needs optimizing. Yes, it really needs  
> > it, I'm not optimizing prematurely. It (mostly) works, it's just way too  
> > slow. And I knew it would be too slow when I wrote it, I just wanted to come  
> > up with a simple implementation first, and get it under test.
> > 
> > That said, I'm wondering if there are any suggestions out there for  
> > tutorials on using ruby-prof. Not tutorials like "here are what the numbers  
> > mean", but "here are some suggestions for looking at the numbers and  
> > deciding where you'll get the most gain in optimizing.
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [15 October 2010 22:02 UTC](https://rubytalk.org/t/process-for-optimizing-ruby-code/60465/3 "2010-10-15T22:02:27Z")

</div>

The biggie for me is to look at the values in the % column and see what the curve is like:

(contrived)

% time call  
36% w  
10% x  
5% y  
1% z

vs:

% time call  
5% w  
4% x  
3% y  
1% z

In the first example, you have something more akin to a power law curve. That says that a majority of the time is being spent in a minority of the calls and is a good candidate for optimizing. You then need to look and see if it is the number of calls that is causing it (which would mean it is a good candidate for memoization or some other form of caching) or if it is from accessing external resources that block or... something. It is also a good candidate to use something like zenprofile's spy\_on method to figure out where the calls are coming from to determine the breakdown. If it is evenly distributed then it might not actually be worthwhile to memoize. It really depends on the design of the code and how it is used.

In the second example, while the times of the profile might be high, the % of time spent is not... graphing that is pretty flat... it is relatively linear with a fairly flat slope.

> **···**
>
> On Oct 15, 2010, at 06:20 , Andrew Wagner wrote:
> 
> > That said, I'm wondering if there are any suggestions out there for  
> > tutorials on using ruby-prof. Not tutorials like "here are what the numbers  
> > mean", but "here are some suggestions for looking at the numbers and  
> > deciding where you'll get the most gain in optimizing.

---

<div class="post-metadata">

**Author:** ![Piotr\_Szotkowski](https://avatars.discourse-cdn.com/v4/letter/p/7cd45c/32.png) [@Piotr\_Szotkowski](https://rubytalk.org/u/Piotr_Szotkowski)\
**Post date:** [3 November 2010 06:48 UTC](https://rubytalk.org/t/process-for-optimizing-ruby-code/60465/4 "2010-11-03T06:48:41Z")

</div>

Andrew Wagner:

> So, I've got a chunk of code which needs optimizing. […]

> That said, I'm wondering if there are any suggestions  
> out there for tutorials on using ruby-prof.

My advice would be to check out perftools.rb¹ and the generated graphs,  
with the additional benefits of it having no profiling overhead and  
being able to see how much time garbage collection takes. You might also  
want to take a look at my Profiling Ruby slides² for example graphs.

¹ [GitHub - tmm1/perftools.rb: gperftools for ruby code](https://github.com/tmm1/perftools.rb)  
² [http://profiling-ruby-rar-2010.heroku.com/](http://profiling-ruby-rar-2010.heroku.com/)

— Piotr Szotkowski

> **···**
>
> --  
> ‘Could we have Math::INFINITY which would make code using it cleaner?’  
> ‘Ruby assumes the environment has native threading and  
> US-ASCII compatible locale. Is there an environment which  
> satisfies the conditions but does not have infinity?’  
> ‘NetBSD on VAX, I guess.’  
> ‘VAX! OK. I’m against introducing Math::INFINITY.’  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[Marc-Andre Lafortune, Yugui, Tanaka Akira, ruby-core]

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [15 October 2010 19:33 UTC](https://rubytalk.org/t/process-for-optimizing-ruby-code/60465/5 "2010-10-15T19:33:59Z")

</div>

Also check if you have: slow DB queries, calls to external services,  
system calls that might be slow.

Loops around any of the issues above.

Jesus.

> **···**
>
> On Fri, Oct 15, 2010 at 4:54 PM, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> 
> > On Fri, Oct 15, 2010 at 3:20 PM, Andrew Wagner \<wagner.andrew@gmail.com\> wrote:
> > 
> > > So, I've got a chunk of code which needs optimizing. Yes, it really needs  
> > > it, I'm not optimizing prematurely. It (mostly) works, it's just way too  
> > > slow. And I knew it would be too slow when I wrote it, I just wanted to come  
> > > up with a simple implementation first, and get it under test.
> > > 
> > > That said, I'm wondering if there are any suggestions out there for  
> > > tutorials on using ruby-prof. Not tutorials like "here are what the numbers  
> > > mean", but "here are some suggestions for looking at the numbers and  
> > > deciding where you'll get the most gain in optimizing.
> > 
> > That's the usual approach when tuning: take the slowest part, make it  
> > faster. Repeat until performance is sufficient - or you run out of  
> > money.
> > 
> > A generally candidates for optimization is object allocation. That is  
> > slow compared to other operations because of the GC management  
> > involved. Also, if you do lookups in huge Array you might want to  
> > switch to indexed access via Hash.

---

<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:** [16 October 2010 09:50 UTC](https://rubytalk.org/t/process-for-optimizing-ruby-code/60465/6 "2010-10-16T09:50:19Z")

</div>

> > That said, I'm wondering if there are any suggestions out there  
> > for tutorials on using ruby-prof. Not tutorials like "here are what  
> > the numbers mean", but "here are some suggestions for looking at  
> > the numbers and deciding where you'll get the most gain in  
> > optimizing.
> 
> The biggie for me is to look at the values in the % column and see  
> what the curve is like:
> 
> (contrived)
> 
> % time call 36% w 10% x 5% y 1% z
> 
> vs:
> 
> % time call 5% w 4% x 3% y 1% z
> 
> In the first example, you have something more akin to a power law  
> curve. That says that a majority of the time is being spent in a  
> minority of the calls and is a good candidate for optimizing. You  
> then need to look and see if it is the number of calls that is  
> causing it (which would mean it is a good candidate for memoization  
> or some other form of caching) or if it is from accessing external  
> resources that block or... something.

Very good advice IMHO!

> In the second example, while the times of the profile might be high,  
> the % of time spent is not... graphing that is pretty flat... it is  
> relatively linear with a fairly flat slope.

Something seems to be missing here. What would be your recommendation in this case? I'd say, tuning is more difficult in this case since you might have to consider architectural changes that affect multiple components. In any case chances are that tuning is more costly than in the first case.

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 16.10.2010 00:02, Ryan Davis wrote:
> 
> > On Oct 15, 2010, at 06:20 , Andrew Wagner wrote:
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)
