How to minimize number of loops?

Discussion in 'PHP' started by KingCobra, Aug 2, 2010.

  1. #1
    <?php

    $s = "2, 5, 7, 4"; //suppose its an array comes from mySql database (never come duplicate value)

    $a = explode(", ", $s);

    echo sizeof($a); // 4 will be printed

    for ($i=0; $i<100; $i++) {
    for ($j=0; $j<sizeof($a); $j++) {
    if ($i==$a[$j]) {
    echo "<br>$i = MATCH";
    }
    }
    }

    ?>


    output:
    4
    2 = MATCH
    4 = MATCH
    5 = MATCH
    7 = MATCH


    The above for loop will run 500 times in total (main loop=100 & sub loop=400 times).

    But is there any way to minimize the loop number and keep the output same?

    Actually I want to compare a group of values (300 items) with another group of values (50 items).
    If I use my script then the loop will run around 15,300 times. But I want to minimize it.

    Note: It is better, if $i matched with any $a[$j] then it will not checkek next $a[$j]

    PLEASE HELP
     
    KingCobra, Aug 2, 2010 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    Can you post your mysql_query

    Otherwise add a limit to the query

    
    LIMIT 0,300
    
    Code (markup):
    Example:
    
    ("SELECT something FROM table LIMIT 0,300");
    
    Code (markup):
    Will return 300 results, starting from 0
     
    MyVodaFone, Aug 2, 2010 IP
  3. Thibaut

    Thibaut Well-Known Member

    Messages:
    886
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    140
    #3
    I agree, you'd rather optimize your SQL query.
     
    Thibaut, Aug 2, 2010 IP
  4. Uninvited

    Uninvited Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    A better idea would be to have those 2 groups sorted and then you check everything in a single loop, with the help of less-than comparisons, like this:

    $i=0;
    $j=0;

    while (($i < 100) && ($j < sizeof($a)))
    {
    if ($i == $a[$j])
    {
    echo "Match ...";
    $i++;
    $j++;
    }
    else if ($i < $a[$j])
    {
    $i++;
    }
    else if ($i > $a[$j])
    {
    $j++;
    }
    }
     
    Uninvited, Aug 2, 2010 IP
  5. KingCobra

    KingCobra Well-Known Member

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #5
    Actually I have 2 table

    Table-1
    field-name-a -------------(6 or more rows)
    ------------
    4
    3
    2
    7
    5
    12

    Table-2
    field-name-b --------------(only 1 row)
    ------------
    2, 5, 7, 4

    SO, what will be the query to print the following output?

    4 = MATCH
    3 = NOT MATCH
    2 = MATCH
    7 = MATCH
    5 = MATCH
    12= NOT MATCH
     
    KingCobra, Aug 2, 2010 IP
  6. Uninvited

    Uninvited Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You can select the row you need from table A, and put it into an array, and the select the only row from table B, and put it into an array too.
    Then, you should sort both of them and then do the check as I mentioned above.

    For example, let's say you have the data in the arrays a and b.

    Then you sort them, and you check for matches:

    
    //----------------------------------------
    // sort a
    for ($i=0; $i<sizeof($a); $i++)
    {
      $min = $i;  
      for ($j=$i+1; $j<sizeof($a); $j++)
      {
          if ($a[$min] > $a[$j]) $min = $j;
      }
    
      $aux = $a[$min];
      $a[$min] = $a[$i];
      $a[$i] = $aux;
    }
    //----------------------------------------
    // sort b
    for ($i=0; $i<sizeof($b); $i++)
    {
      $min = $i;  
      for ($j=$i+1; $j<sizeof($b); $j++)
      {
          if ($b[$min] > $b[$j]) $min = $j;
      }
    
      $aux = $b[$min];
      $b[$min] = $b[$i];
      $b[$i] = $aux;
    }
    //----------------------------------------
    // check for matches
    $i=0;
    $j=0;
    
    while (($i < sizeof($a)) && ($j < sizeof($b)))
    {
      if ($a[$i] == $b[$j])
      {
        echo "Match ...";
        $i++;
        $j++;
      }
      else if ($a[$i] < $b[$j])
      {
         $i++;
      }
      else if ($a[$i] > $b[$j])
      {
         $j++;
      }
    } 
    
    PHP:
     
    Uninvited, Aug 2, 2010 IP
  7. Uninvited

    Uninvited Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Sure, if you want it to be efficient, you should use another method for sorting, not the one I used above. Or you can use the php function sort().
     
    Uninvited, Aug 2, 2010 IP