Jump to content

[SOLVED] too much to process


jakebur01

Recommended Posts

How can I optimize this to make it run better. As of now it will not even do a few zip codes. Is there any way to like sleep in between each loop or something?

 $result = mysql_query("SELECT * FROM zip_code where `state_prefix` = 'OK'", $db) or die(mysql_error());
  
  


while($myrow = mysql_fetch_array ($result))
{

$zip=$myrow['zip_code'];


$html = file_get_html('http://www.my-site.com/test.asp?transaction=search&template=map_search&search1=0&pwidth=400&pheight=700&proxIconId=400&proxIcons=1&search2=0&search3=1&country=US&searchQuantifier=AND&address=&city=&stateProvince=+&postalCode=$zip&radius=500&x=78&y=16');

$i = 0;
$tmp = $html->find('span[class=mqEmp], span[class=Black11]');
$cnt = count($tmp) - 1;
foreach($tmp as $e) {
    if($i > 0 && $i < $cnt){
        echo $e->plaintext . '<br>';
    }
    $i++;
}


}

 

 

Link to comment
Share on other sites

The main problem as far as efficiency goes with your code is that you have a loop inside a loop inside a loop.

 

Think of it like this;

 

Loop a goes around 10 times

    Loop b goes around 5 times

        Loop c goes around 10 times

            //code

        End loop c

    End loop b

End Loop a

 

Those loops will be run through a total of 500 times despite the small number in each loop's stop condition. Adding 1 to the number of times any one of those loops will cause the total number of times the code is executed to rocket.

 

Loops inside loops should be avoided where possible and loops inside loops that are inside loops should definitely be avoided - but unfortunately, it's not always possible.

 

 

As far as your code goes: I have no idea what your code is supposed to do, so I can't really advise as to how to make your code more efficient.

Link to comment
Share on other sites

your loops are fine.. i use 3-5 nested loops regularly but as jake pointed out above, you need to control your resultset.  only limit your results to 20-50 and make a (next,previous) button to provide a method to the user to get the next set of results.

Link to comment
Share on other sites

It is not to bad if I limit it by one state at a time. It is actually faster than I expected after I fixed that little quote issue.

 

I have search and not been able to figure how to tab after x number of loops when writing to a text file. I would like to write 8 fields then tab to a new line. Something like:

$i = 0;
$tmp = $html->find('span[class=mqEmp], span[class=Black11]');
$cnt = count($tmp) - 1;
foreach($tmp as $e) {
    if($i > 0 && $i < $cnt){

        $outputstring=$e->plaintext . '\t';

	 fwrite($fp, $outputstring, strlen($outputstring));

// if eighth field, then start a new row    something like:  fwrite($fp, \n);
            

    }
    $i++;
}



 

 

Link to comment
Share on other sites

instead of tabbing to a new line why not use the new line, character feed???  \n\r

 

Edit:  remember that tabs have nothing to do with new lines.  the fact that some text editors WRAP lines after a certain amount of characters has nothing to do with line termination, has to do with visible line space.  =D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.