# Speed up recursive loop or get rid of it?

**URL:** <https://www.ruby-forum.com/t/speed-up-recursive-loop-or-get-rid-of-it/168322>\
**Category:** Rails\
**Created:** [May 19, 2009, 4:57pm UTC](https://www.ruby-forum.com/t/speed-up-recursive-loop-or-get-rid-of-it/168322 "2009-05-19T16:57:08Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [May 19, 2009, 4:57pm UTC](https://www.ruby-forum.com/t/speed-up-recursive-loop-or-get-rid-of-it/168322/1 "2009-05-19T16:57:08Z")

</div>

so…

I’m making a scheduler… i have a table of scheduled and scheduleable  
hours for multiple users which needs to be turned into a view (with  
schedule type as there are 4 different types) of 7 days of those  
scheduled and scheduleable hours for multiple users…

right now i’ve a loop which iterates over every hour and tests it  
against all the returned scheduleable and scheduled hours for those 7  
days…

it’s really slow… the loop that figures out the schedule type runs like  
1000 times and just obliterates my processor locally while it’s running.

anyone have any suggestion of how to speed things up? i’ve been  
thinking about it but can’t seem to come to a speedier conclusion.

---

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [May 19, 2009, 5:57pm UTC](https://www.ruby-forum.com/t/speed-up-recursive-loop-or-get-rid-of-it/168322/2 "2009-05-19T17:57:51Z")

</div>

Morgan -

On 19-May-09, at 10:57 AM, Morgan M. wrote:

> against all the returned scheduleable and scheduled hours for those 7  
> days…
> 
> it’s really slow.. the loop that figures out the schedule type runs  
> like  
> 1000 times and just obliterates my processor locally while it’s  
> running.

take a look at the runt gem..runt is a temporal expression api

[http://runt.rubyforge.org/doc/files/doc/tutorial\_schedule\_rdoc.html](http://runt.rubyforge.org/doc/files/doc/tutorial_schedule_rdoc.html)

[http://runt.rubyforge.org/doc/files/doc/tutorial\_te\_rdoc.html](http://runt.rubyforge.org/doc/files/doc/tutorial_te_rdoc.html)

Jodi

---

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [May 19, 2009, 6:56pm UTC](https://www.ruby-forum.com/t/speed-up-recursive-loop-or-get-rid-of-it/168322/3 "2009-05-19T18:56:13Z")

</div>

On 20/05/2009, at 12:57 AM, Morgan M.  
\<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> wrote:

> against all the returned scheduleable and scheduled hours for those 7

My suggestion is to drive things from the view requirements. What does  
your view actually need to output? From there, work backwards to the  
code required.

If you give us morecontect, we can help further.

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Twitter: [http://twitter.com/random8r](http://twitter.com/random8r)  
Learn: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)  
New video up now at [http://sensei.zenunit.com/](http://sensei.zenunit.com/) real fastcgi rails  
deploy process! Check it out now!

---

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [May 19, 2009, 7:21pm UTC](https://www.ruby-forum.com/t/speed-up-recursive-loop-or-get-rid-of-it/168322/4 "2009-05-19T19:21:45Z")

</div>

Julian L. wrote:

> On 20/05/2009, at 12:57 AM, Morgan M.  
> \<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> 
> > wrote:
> 
> > against all the returned scheduleable and scheduled hours for those 7
> 
> My suggestion is to drive things from the view requirements. What does  
> your view actually need to output? From there, work backwards to the  
> code required.
> 
> If you give us morecontect, we can help further.
> 
> Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
> Twitter: [http://twitter.com/random8r](http://twitter.com/random8r)  
> Learn: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)  
> New video up now at [http://sensei.zenunit.com/](http://sensei.zenunit.com/) real fastcgi rails  
> deploy process! Check it out now!

it’s basically just a calendar view from 8am to 12pm with colours for  
each of the schedule types returned

so it’s a grid

b = blue  
r = red  
there are actually 5 colours but in the interest of berevity i chose  
two.

```
  8:00 9:00 10:00 11:00 12:00 1:00 2:00 3:00 4:00 5:00 

```

6:00 7:00  
name0 b b r r r b r b b r b  
r  
name1 b b r r r b r b b r b  
r  
name2 b b r r r b r b b r b  
r  
name3 b b r r r b r b b r b  
r  
name4 b b r r r b r b b r b  
r  
name5 b b r r r b r b b r b  
r  
name6 b b r r r b r b b r b  
r

this represents one of the 7 days and the type of schedule is determined  
in a db table.

---

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [May 19, 2009, 7:25pm UTC](https://www.ruby-forum.com/t/speed-up-recursive-loop-or-get-rid-of-it/168322/5 "2009-05-19T19:25:34Z")

</div>

Morgan M. wrote:

> it’s basically just a calendar view from 8am to 12pm with colours for  
> each of the schedule types returned
> 
> so it’s a grid

Can you change the model then? It sounds like something easier done the  
other way around, ie, you iterate through the things that are scheduled  
for when they are scheduled, and get rid of the calendar in the model –  
just implement it in the view based on the database of things scheduled.

---

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [May 19, 2009, 7:29pm UTC](https://www.ruby-forum.com/t/speed-up-recursive-loop-or-get-rid-of-it/168322/6 "2009-05-19T19:29:45Z")

</div>

On Tue, May 19, 2009 at 8:57 AM, Jodi S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > hours for multiple users which needs to be turned into a view (with  
> > running.
> 
> take a look at the runt gem..runt is a temporal expression api
> 
> [http://runt.rubyforge.org/doc/files/doc/tutorial\_schedule\_rdoc.html](http://runt.rubyforge.org/doc/files/doc/tutorial_schedule_rdoc.html)
> 
> [http://runt.rubyforge.org/doc/files/doc/tutorial\_te\_rdoc.html](http://runt.rubyforge.org/doc/files/doc/tutorial_te_rdoc.html)
> 
> Jodi

Jodi, thanks for posting the above two links and I’m sure that others  
will  
find them helpful as well.

-Conrad

---

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [May 19, 2009, 9:27pm UTC](https://www.ruby-forum.com/t/speed-up-recursive-loop-or-get-rid-of-it/168322/7 "2009-05-19T21:27:14Z")

</div>

Julian L. wrote:

> On 20/05/2009, at 3:21 AM, Morgan M.  
> \<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> 
> > wrote:
> 
> > r  
> > r b
> 
> So you have a model hour, and it belongs to a model hour type and also  
> belongs to a user?
> 
> Then your controller loads users, :include =\> hours and hour type
> 
> Then, your view loops the users one row per user, loops one cell per  
> hour, and prints the type.
> 
> Simple?!
> 
> Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
> Twitter: [http://twitter.com/random8r](http://twitter.com/random8r)  
> Learn: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)  
> New video up now at [http://sensei.zenunit.com/](http://sensei.zenunit.com/) real fastcgi rails  
> deploy process! Check it out now!

hrmm. i guess it would make more sense to do a bunch of db querys and  
parse small amounts of data instead of doing one big db call and parsing  
large amounts of data.. i’ll see if calms it down when it’s more  
balanced.

thank you.

---

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [May 19, 2009, 7:43pm UTC](https://www.ruby-forum.com/t/speed-up-recursive-loop-or-get-rid-of-it/168322/8 "2009-05-19T19:43:56Z")

</div>

On 20/05/2009, at 3:21 AM, Morgan M.  
\<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> wrote:

> r  
> r b

So you have a model hour, and it belongs to a model hour type and also  
belongs to a user?

Then your controller loads users, :include =\> hours and hour type

Then, your view loops the users one row per user, loops one cell per  
hour, and prints the type.

Simple?!

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Twitter: [http://twitter.com/random8r](http://twitter.com/random8r)  
Learn: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)  
New video up now at [http://sensei.zenunit.com/](http://sensei.zenunit.com/) real fastcgi rails  
deploy process! Check it out now!

---

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [May 19, 2009, 10:22pm UTC](https://www.ruby-forum.com/t/speed-up-recursive-loop-or-get-rid-of-it/168322/9 "2009-05-19T22:22:23Z")

</div>

Morgan M. wrote:  
[…]

> hrmm. i guess it would make more sense to do a bunch of db querys and  
> parse small amounts of data instead of doing one big db call and parsing  
> large amounts of data..

No. Ask for all the records you need in as few queries as possible  
(remember that AR will take arrays of IDs, as well as all manner of  
other conditions). Many little queries is generally a sign that  
something is wrong.

## Best,

Marnen Laibow-Koser

> **[marnen.org: Homepage of Marnen Laibow-Koser](https://marnen.org/)**

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

---

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [May 20, 2009, 1:00am UTC](https://www.ruby-forum.com/t/speed-up-recursive-loop-or-get-rid-of-it/168322/10 "2009-05-20T01:00:23Z")

</div>

Morgan M. wrote:  
[…]

> well say i had 25 users, i’d be doing 25 querys right. one for each  
> user then looping through the results in basically the same way i was  
> doing it before?

Gosh, I hope not. 🙂 Fetch it all in one query with lots of joins, then  
loop through the recordset in memory.

In general, DB queries don’t belong inside loops. Use the power of the  
database instead!

## Best,

Marnen Laibow-Koser

> **[marnen.org: Homepage of Marnen Laibow-Koser](https://marnen.org/)**

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

---

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [May 20, 2009, 12:44am UTC](https://www.ruby-forum.com/t/speed-up-recursive-loop-or-get-rid-of-it/168322/11 "2009-05-20T00:44:32Z")

</div>

Marnen Laibow-Koser wrote:

> Morgan M. wrote:  
> […]
> 
> > hrmm. i guess it would make more sense to do a bunch of db querys and  
> > parse small amounts of data instead of doing one big db call and parsing  
> > large amounts of data..
> 
> No. Ask for all the records you need in as few queries as possible  
> (remember that AR will take arrays of IDs, as well as all manner of  
> other conditions). Many little queries is generally a sign that  
> something is wrong.
> 
> ## Best,
> 
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

well say i had 25 users, i’d be doing 25 querys right. one for each  
user then looping through the results in basically the same way i was  
doing it before?
