Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to iterate through a "window" of data in a dataset?
    primarykey
    data
    text
    <p>I have a data set in mysql with 150 rows. I have a set of 2 for loops that run math calculations based on some user inputs and the dataset. The code does calculations for 30 row windows, and accumulates the results for each 30 row window in an array. What I mean is, I do a "cycle" of calculations on rows 0-29, then 1-30, then 2-31, etc... That would result in 120 "cycles". </p> <p>Right now the for loop is set up like so (there are more fields, I just trimmed the code for simplicity of this question.</p> <pre><code> $period=30; $query = "SELECT * FROM table"; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)){ $data[] = array("Date" =&gt; $row['Date'], "ID" =&gt; $row['ID']); } for($i=0;$i&lt;(count($data)-$window);$i++){ for($j=0;$j&lt;$window;$j++){ //do calculations here with $data[] $results[$i][$j]= calculations; } } </code></pre> <p>This works fine for the number of rows I have. However, I opened up the script to a larger dataset (1700 rows) with a different window (360 rows). This means there are exponentially more iterations. It gave me an out of memory error. Some quick use of memory_get_peak_usage() showed that memory would just continually increase.</p> <p>I'm starting to think that having the loops search through that data array is extremely laborious, especially when the "window" overlaps on a lot of the "cycles". Example: Cycle 0 goes through rows 0-29. Cycle 1 goes through rows 1-30. So, both of those cycles share a row of data that they need, but I'm telling PHP to look for the new data each time.</p> <p>Is there a way to structure this better? I'm getting kind of lost thinking about running these concurrent cycles.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload