By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,209 Members | 1,706 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,209 IT Pros & Developers. It's quick & easy.

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

P: n/a

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();
}

?>

Aug 21 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 21 Aug 2006 15:50:59 -0700, "lawrence k" <lk******@geocities.comwrote:
>The following function is way too slow. If anyone has any suggestions
about how to speed it up, I'd be grateful for them.
Nobody can run your code as posted, so you have to do some work first. Find
which bits of your code are slow, and start drilling down into the definitions.

You can time sections of code with http://uk.php.net/microtime

Or perhaps:

http://pear.php.net/package/Benchmar..._Profiler.html

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Aug 21 '06 #2

P: n/a
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?"
<cut>php script(/cut>

Lawrence,

My guess is that your problems are in the database related queries and
the way you prefer to execute and handle them.
I prefer doing things in the database, as they are made for selecting,
sorting and organizing. They are probably more memory and speed
efficient than doing it in your own code.

In my opinion it is useless to query all bands (the result set will
probably be the whole table of bands) and then select the bands
starting with a certain letter using you program logic.

If you want to be sure you can write a script of course which times the
display and execution of 36 or so queries to get all characters from the
database and group them per character or query all the bands and display
them in groups using your program code. My guess is that your program
logic will consume (much) more time as you have to check whether the
first character of the current record differs from the one before.

Perhaps it is also a good idea to spend some time on the way you
organize your database and define it's indexes, as the latter can really
improve the speed of your queries. Holding all the data in one big table
is also a bad idea, normalize your data so to minimize duplicate data in
your database, this will also improve indexing.

A few examples of search examples using the (my)SQL syntax:

example 1:

SELECT * FROM bands WHERE LOWER(bandname) LIKE 'a';

The above example will simply select every band starting with a lower or
capital a.

example 2:

BANDS table:
id_band
bandname
id_genre

GENRE table:
id_genre
genrename

If you have normalized your tables (for information aboout normalization
for databases, please use google, it willl give you plenty of good
results) you would perhaps have a table which will hold all the genres
and another for all the bands. Each band will have a genre associated
with it in the bands table which can be linked to the genre table using
queries like this:

SELECT bands.bandname, genre.genrename
FROM bands
JOIN genre
ON genre.id_genre = bands.id_genre
WHERE genre.genrename = 'grunge' OR genre.genrename = 'punk';

Above query will result in a two columns result set with the name of the
band in one and the associated genre (either punk or grunge) in the
second column.

A third table could hold all the album data which should be related to
the bands table, but can also be related to the genre table or some
other table holding producers, recording studio or what so ever.

There probably is a newsgroup/forum for your preferred database backend.
If you are looking for MySQL groups news://mailing.database.mysql
would be a good one to start.

I know I went a bit OT, but who knows it might help you improve the
speed of your code as well.

Kind regards,

Jonathan
Aug 22 '06 #3

P: n/a

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.

Aug 22 '06 #4

P: n/a
Andy Hassall wrote:
<snip>
http://pear.php.net/package/Benchmar..._Profiler.html
Out of interest... I'm just curious, are you really using this
package?

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Aug 26 '06 #5

P: n/a
On 26 Aug 2006 13:28:33 -0700, "R. Rajesh Jeba Anbiah"
<ng**********@rediffmail.comwrote:
>Andy Hassall wrote:
<snip>
>http://pear.php.net/package/Benchmar..._Profiler.html

Out of interest... I'm just curious, are you really using this
package?
Nope; just looked like something the OP might want to try.

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Aug 26 '06 #6

P: n/a
Andy Hassall wrote:
On 26 Aug 2006 13:28:33 -0700, "R. Rajesh Jeba Anbiah"
<ng**********@rediffmail.comwrote:
<snip>
http://pear.php.net/package/Benchmar..._Profiler.html
Out of interest... I'm just curious, are you really using this
package?

Nope; just looked like something the OP might want to try.
(Apologies for late response; had connectivity problem.) The
script's method look unnecessary overkill to me. The solution based on
ticks (though it won't work in Windows) <http://in2.php.net/declare>
and further refinements by Chung Leong looks better than this one. I'm
still after APD even though it's kinda dead.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Aug 28 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.