This is my code:
$size_all_colonies = sizeof($all_colonies) ;
$size_all_coods = sizeof($all_cood) ;
for ( $i = 0 ; $i < $size_all_colonies ; $i++ )
{
$id = $all_colonies[$i]['id'] ;
$colony_name = $all_colonies[$i]['colony_name'] ;
$colony_node = $dom->createElement('colony');
$response->appendChild($colony_node);
$colony_text_node = $dom->createTextNode($colony_name) ;
$colony_node->appendChild($colony_text_node) ;
$id_node = $dom->createElement('id_node') ;
$colony_node->appendChild($id_node) ;
$id_node->appendChild($dom->createTextNode($id)) ;
$coods = "" ;
for ( $j = 0 ; $j < $size_all_coods ; $j++ )
{
if ( $id == $all_cood[$j]['colony_id'] )
{
$coods .= $all_cood[$j]['latitude'].",".$all_cood[$j]['longitude'].";" ;
// break ;
}
}
$coods =substr($coods,0,-1);
$cood_node = $dom->createElement('cood_node') ;
$colony_node->appendChild($cood_node) ;
$cood_node->appendChild($dom->createTextNode($coods)) ;
echo $id . ' ' ;
}
Explanation:
all_colonies = is a multi-dimensional array – size- 7000
all_coods – another multi array – size 70000
all_cood[$j][‘colony_id’] is foreign key and corresponds to all_colonies[$i][‘id’].
This is constructing a response for an ajax request, which ideally should take a few minutes, but is taking a few hours 🙁 . And it slows down gradually, like the first 1000 loops/iterations (outer for) happen in under 30 seconds, but the next thousand takes minutes, and the next thousand forever.
Please help!
Thanks
edit: the break statement was not required! I added when I was desperate to improve the performance, but it should not be there..
Something you could try is setting up the all_coods variable to be indexed by the id, rather than just being an array. This way you will save time with the inner loop.
For example: