Note that there are some explanatory texts on larger screens.

plurals
  1. POComparing Time Values To Find New Records Taking Forever
    text
    copied!<p>Okay. So I've been pulling my hair out trying to figure out why my script is taking forever to run. I've pinpointed a potential cause and would like to figure out a more efficient way of performing this following functionality:</p> <pre><code>function getwrs($obj, $recs) { foreach ($obj as $key =&gt; $val) { for ($i=0; $i&lt;count($recs); $i++) { $wr = array(); if ($recs[$i][0] === $key) { $wr = $recs[$i]; break; } } if (count($wr) &gt; 0) { for ($i=0; $i&lt;count($val); $i++) { if ($val[$i]["time"] &lt; $wr[1] &amp;&amp; abs($wr[1] - $val[$i]["time"]) &lt; 30) { $uwrs[] = array($val[$i], $wr); } } } } return $uwrs; } </code></pre> <p>I'm calling this function like so:</p> <pre><code>$wrs = getwrs($top15["pro"], $recs); </code></pre> <p>where <code>$top15["pro"]</code> is a multidimensional array with the following structure:</p> <pre><code>[MAP1NAME] =&gt; Array ( [0] =&gt; Array ( [authid] =&gt; XXXX [name] =&gt; XXXX [time] =&gt; XXXX [date] =&gt; XXXX ... ) ) [MAP2NAME] =&gt; Array ( [0] =&gt; Array ( [authid] =&gt; XXXX [name] =&gt; XXXX [time] =&gt; XXXX [date] =&gt; XXXX ... ) [1] =&gt; Array ( [authid] =&gt; XXXX [name] =&gt; XXXX [time] =&gt; XXXX [date] =&gt; XXXX ... ) ... ) </code></pre> <p>and contains just over 1500 entries, and $recs is an array containing comparable values and about 3000 entries with the following structure:</p> <pre><code>[0] =&gt; Array ( [0] =&gt; MAP1NAME [1] =&gt; TIMEVAL [2] =&gt; USERNAME1 [3] =&gt; COUNTRY ) ... </code></pre> <p>The purpose of this function is to find unofficial world records by comparing time values for each map in my database (<code>$top15</code>) with time values from an official world records array (<code>$recs</code>).</p> <p>Right now I take each map name from my database and find the same string from the official world records array, then store the official world record values for the map in a variable <code>$wr</code> if there is an official record. Next I loop through the array of completed times from my database to find which ones, if any, are less than the official record time. Each time this is true I push both the info pertaining to my database time and the official record time (user, time, etc.) to a new array. This is repeated for every key (map name) from my database. Finally, the function returns my unofficial world records array (<code>$uwrs</code>).</p> <p>The problem is simply: this takes too damned long for some reason. I originally coded my script in JavaScript and it was quite responsive, so I'm maybe thinking I'm doing something wrong here. Any help?</p> <p>Edit: I've used xdebug to pinpoint the problem and when I add the call to this function my output file goes from 3MB to over 30MB and the script run time goes from about 1 second to 15-20s. I'm relatively new to this tool so I don't know if this information is important or not.</p> <p>Edit 2: as per request the structure of my database is as follows:</p> <pre><code>map | authid | name | time | date | weapon </code></pre> <p>ex:</p> <pre><code>kzbr_brickngrasshop | STEAM_0:1:XXXXXXX | John Travolta kz-endo | 23.582824 | 2013-08-25 | 03:40:17 | knife </code></pre>
 

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