Forum: Ruby on Rails Speed up recursive loop or get rid of it?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Morgan M. (Guest)
on 2009-05-19 18:57
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.
Jodi S. (Guest)
on 2009-05-19 19:57
(Received via mailing list)
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_s...

http://runt.rubyforge.org/doc/files/doc/tutorial_t...

Jodi
Julian L. (Guest)
on 2009-05-19 20:56
(Received via mailing list)
On 20/05/2009, at 12:57 AM, Morgan M.
<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/
Twitter: http://twitter.com/random8r
Learn: http://sensei.zenunit.com/
New video up now at http://sensei.zenunit.com/ real fastcgi rails
deploy process! Check it out now!
Morgan M. (Guest)
on 2009-05-19 21:21
Julian L. wrote:
> On 20/05/2009, at 12:57 AM, Morgan M.
> <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/
> Twitter: http://twitter.com/random8r
> Learn: http://sensei.zenunit.com/
> New video up now at 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.
Mk 2. (Guest)
on 2009-05-19 21:25
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.
Conrad T. (Guest)
on 2009-05-19 21:29
(Received via mailing list)
On Tue, May 19, 2009 at 8:57 AM, Jodi S. <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_s...
>
> http://runt.rubyforge.org/doc/files/doc/tutorial_t...
>
> Jodi
>

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

-Conrad
Julian L. (Guest)
on 2009-05-19 21:43
(Received via mailing list)
On 20/05/2009, at 3:21 AM, Morgan M.
<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/
Twitter: http://twitter.com/random8r
Learn: http://sensei.zenunit.com/
New video up now at http://sensei.zenunit.com/ real fastcgi rails
deploy process! Check it out now!
Morgan M. (Guest)
on 2009-05-19 23:27
Julian L. wrote:
> On 20/05/2009, at 3:21 AM, Morgan M.
> <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/
> Twitter: http://twitter.com/random8r
> Learn: http://sensei.zenunit.com/
> New video up now at 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.
Marnen L. (Guest)
on 2009-05-20 00:22
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
removed_email_address@domain.invalid
Morgan M. (Guest)
on 2009-05-20 02:44
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
> 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?
Marnen L. (Guest)
on 2009-05-20 03:00
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
http://www.marnen.org
removed_email_address@domain.invalid
This topic is locked and can not be replied to.