Re: my code is too slow, how do I speed it up?




lawrence k wrote:
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;


I think that you're going about caching the wrong way in this instance.
The bands database could grow very, very large, and storing all of
that in PHP could easily become prohibitive. But, if you're going to
be selecting everything from the database every page load (a Bad Idea),
then we'll go with that:

Since you're going to be searching all the data in memory, you should
store it in a structure that provides a quick way of searching (a tree
or heap would be a good idea in this case). This can easily reduce
your search time, per letter, from O(N) to O(log N). If you aren't
familar with what that means, basically think of it as an expontial
speedup.

It will be easy to either write your own or find one.

.



Relevant Pages

  • Re: Binary tree
    ... I am using a static array for a database that will be frequently ... searching / updated, but i would like to move to balance binary ... I could recommend a couple of self balancing trees. ...
    (comp.lang.c)
  • RE: Database running over network runs extremely slow
    ... I have reports built into the database that track the entry ... you are searching (or using in your DLOOKUPs criteria parameter)? ... of the DLookups to looped recordset searches, ...
    (microsoft.public.access.tablesdbdesign)
  • Re: How many names and addresses can you have for ID card
    ... large amount of database space, ... Searching CLOBs is thus memory and CPU-intensive, ... A directory system a la LDAP or X.500 would be a much more natural ... An entry may have any number of atrributes which are ...
    (uk.legal)
  • Re: Exporting Text field line entries to individual records
    ... Been doing alot of searching on this group, and have not come up with ... Stuff that is entered in the field, followed by a carriage return: ... manually, except that are thousands of SP's in this database, and some ... calculation field that selects text between carriage returns. ...
    (comp.databases.filemaker)
  • Re: Is there anynone who knows a chatting site or someting like this? - Face Matching
    ... and then you would search your database to find matching ... advance by searching the internet retrieving ... URL and the feature vector to your database. ... You would compare the ...
    (sci.image.processing)