Topic: Speed of loop through Time object

I recently just posted here to try to pinpoint where my application was hanging up at. After a few tests in the console I found that the culprit lies in incrementing the loop. This is the code I am using:

date =
start_date = 3.months.ago
interval = (date-start_date)/10
( do |cur_time|
    # A single DB query here that takes 1.5ms

The total running time ended up being 542570ms. I tried this in the console replacing the DB query with a puts "test" and found that the time between the puts was ages. If I increased the interval divider (e,g 10 to say 500) it had a quicker frequency but still ran in the same amount of total time. Is there a quicker way to step through this Time object?


Re: Speed of loop through Time object

Well I'm not entirely sure why this works but now the page loads nearly instantly. All I did was change the time objects to integer ones using Time.to_i so now the loop uses integers rather than a Time object to iterate on through. In my loop I then had to convert the integer back to a time which I used

Here is the new code:

date_i = date.to_i
interval = (date_i - start_date_i)/500

(start_date_i..date_i).step(interval) do |cur_time|
     # DB query that converts back to Time using

Last edited by joesepth (2009-08-25 20:19:59)