You are here: Re: my code is too slow, how do I speed it up? « PHP Programming Language « IT news, forums, messages
Re: my code is too slow, how do I speed it up?

Posted by Richard Levasseur on 08/22/06 16:02

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.

 

Navigation:

[Reply to this 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

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