Re: my code is too slow, how do I speed it up?
- From: "Richard Levasseur" <richardlev@xxxxxxxxx>
- Date: 22 Aug 2006 09:02:33 -0700
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.
.
- References:
- my code is too slow, how do I speed it up?
- From: lawrence k
- my code is too slow, how do I speed it up?
- Prev by Date: Re: sorting numbers
- Next by Date: Re: How do I set Session Variables
- Previous by thread: Re: my code is too slow, how do I speed it up?
- Next by thread: Scope of php variables outside <?php ?> blocks
- Index(es):
Relevant Pages
|