2

I have an array that is supposed to contain 100,000 items. Here is what my algorithm looks like:

    $holder = array();
if($result->num_rows > 0) 
        {
            while($row = $result->fetch_assoc()) 
            {

            for($i = 0; $i < mysqli_num_rows($result); $i++)
                {
                    $x = 0;
                    if($holder[$i]['key'] == $row["key"] && $holder[$i]['value'] == $row["value"] )
                    {
                        $x++;
                    }

                    if($x == 0)
                    { 
                        $holder[$row["key"]] = $row["value"];
                    }
                }}
    }

So what it does is that it selects 'key' and 'value' from database, checks if the 'key' and 'value' occur together in this array, if they do, then do nothing, else, adds this 'key'=> 'value' to the array. The problem is that for a 100,000 lines, it takes ages to complete this process and I am in need of a quicker algorithm that does this task in short time. Any suggestions will be highly appreciated.

  • 1
    Can you edit your question to include what `$holder` looks like before the operation, what the MySQL result set looks like, and what `$holder` looks like after the operation? It seems odd that you're mixing `$holder[$i]['key']` (integer keys) and `$holder[$row['key']]` (string keys—or does the `key` column contain integers too?), but maybe I'm just misunderstanding your aim. – Jordan Running Jul 23 '15 at 16:33
  • Perhaps you could use fetch row and benchmark yourself? What is faster. Or You can first receive all results in a associative local array and then let the outer loop run over this. – icbytes Jul 23 '15 at 16:34
  • Depending on your query, and what you want as output (which appears just to be a count by key and value, you can probably do this through your query anyway – Mark Baker Jul 23 '15 at 16:35
  • How long is "ages"? How long does it take, and how long would you like it to take? – Andy Lester Jul 23 '15 at 16:41
  • 1
    Do you know what lines of code are taking the longest to execute? Use a tool like xdebug (http://xdebug.org) to find out what code is taking the longest. You might find, for example, that the array manipulation is not the bottleneck, but rather that the repeated calls to `mysqli_num_rows` is what is taking so long. Without profiling the code, you don't know what is slow. – Andy Lester Jul 23 '15 at 16:42
  • http://stackoverflow.com/questions/22769666/to-preallocate-or-not-to-preallocate-lists-in-python might be useful - "or not". – greybeard Jul 23 '15 at 16:43
  • 3
    eliminate the entire concept of loading a whole table into an array if you can – developerwjk Jul 23 '15 at 16:44

1 Answers1

0

the original code you posted seems bugged .. but i'll post the algorithm you described

$holder = array();                                 //the array to be filled
if($result->num_rows > 0)                          //if database returned results 
    while($row = $result->fetch_assoc())           //for each returned result
        if(!isset($holder[$row["key"]]))           //if result not already in array
            $holder[$row["key"]] = $row["value"];  //set it

this should be the fastest you can get, simply by optimizing code.. faster would require other methods

CaldasGSM
  • 3,032
  • 16
  • 26