Reply to my code is too slow, how do I speed it up?

Your name:

Reply:


Posted by lawrence k on 08/21/06 22:50

The following function is way too slow. If anyone has any suggestions
about how to speed it up, I'd be grateful for them. We have to call
this function 36 times on one page, and I think each time it takes half
a second, so it adds up to maybe 18 seconds, which is a lot when you're
showing software to a client. The reponse we get is "Why is it so
slow?"


<?php


function showBandAlpha($firstFewCharsWeWantToMatch=false,
$fieldToGet="cbHeadline") {
// 05-22-06 - I'm working with Hoyt on www.monkeyclaus.org, he wants
to list all the
// music bands, segmented by the first letter, so we need to all the
bandnames as an array,
// and then checked against the first letter. That is, he wants to
be able to show all
// the music bands whose name starts with the letter "A" by putting
this line in his design:
//
// showBandAlpha("a");
//
// and all the bands whose name starts with the letter "b" should
appear wherever Darren puts the
// line:
//
// showBandAlpha("b");

$cfa = & getController();

$arrangementObject = & $cfa->getObject("McArrangements",
"showBandAlpha");
$selectObject = & $cfa->getObject("ExteriorSelect",
"showBandAlpha");


// 08-21-06 - for the sake of speed, we only want to make this call
to the database once,
// so we get all the names of all the music bands and store it in a
static array. We will
// probably all this function 36 times on one page - once for each
letter, and then for
// each number 0-9. Rather than calling the database 36 times for
the same information,
// we call it once and store the info in a static array.
static $bandNamesArray;

if (!is_array($bandNamesArray)) {
$selectObject->begin("showBandAlpha");
$selectObject->setDatastoreObject();
$selectObject->setQueryObject("GetAllOfType");
$selectObject->setInfoToBeSought("weblogPages");
$selectObject->getInfo("noLimit");
$howMany =$selectObject->getCountOfReturn();

for ($i=0; $i < $howMany; $i++) {
$row = $selectObject->getRow();
$bandNamesArray[] = $row;
}
}


// 08-21-06 - this function was first written so as to match the
first letter of a
// string. We then got a request to match strings like "blues",
"punk", and "grunge".
$lengthOfCharsWeWantToMatch = strlen($firstFewCharsWeWantToMatch);

if (is_array($bandNamesArray)) {
// 08-21-06 - now we are going to loop through the array that
holds all the band
// names and we are going to pick out the ones whose begining
matches the letter
// or string that we are suppose to match against. For instance,
a music band with
// the name "Amazons" will be picked out if the designer has put
the line
// showBandAlpha("a") on the page somewhere.
for ($i=0; $i < count($bandNamesArray); $i++) {
$row = $bandNamesArray[$i];
$val = $row[$fieldToGet];

$beginningOfStringFromEntry = substr($val, 0,
$lengthOfCharsWeWantToMatch);
if (stristr($beginningOfStringFromEntry,
$firstFewCharsWeWantToMatch)) {
$arrangementObject->setEntry($row);
$cfa->command("GetArrangement", "listBandsToEdit");
}
}
} else {
$cfa->error("In showBandAlpha we were not able to get an array of
band names.", "showBandAlpha");
}

// 08-11-06 - the database calls are taking too much memory. We need

// to close it down.
$selectObject->close();
}



?>

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация