Speed up recursive loop or get rid of it?

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.

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_te_rdoc.html

Jodi

On 20/05/2009, at 12:57 AM, Morgan M.
<[email protected]

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!

Julian L. wrote:

On 20/05/2009, at 12:57 AM, Morgan M.
<[email protected]

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.

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.

On Tue, May 19, 2009 at 8:57 AM, Jodi S. [email protected] 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_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

Julian L. wrote:

On 20/05/2009, at 3:21 AM, Morgan M.
<[email protected]

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.

On 20/05/2009, at 3:21 AM, Morgan M.
<[email protected]

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. 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
[email protected]

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. :slight_smile: 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
[email protected]

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
[email protected]

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?