473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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


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
6 2065
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
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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Tzu-Chien Chiu | last post by:
Hi, "What methods do you ever use to optimize the programs?" We're developing a graphics chip emulator in C++, but it's very slow for big scenes. Even though this is a cross-platform software,...
9
by: Neil | last post by:
I've been discussing here a SQL 7 view which scrolls slowly when linked to an Access 2000 MDB. After trying various things, I've distilled it down to the following: when the linked view has a...
2
by: Robert Hooker | last post by:
Hi, I'm curious to know if I'm doing something wrong here, or if this is just mind-numbingly slow for a reason. In a simple WindowsFormsApplication: public Form1() { // Required for...
12
by: Aykut Canturk | last post by:
We perfectly develop our applicaitons with VS 6.0. We want to move to .net but all of our development computers almost die with a small test application. we have min P4 2ghz and 512MB desktop PCs...
3
by: LDD | last post by:
Hi Folks I've recently upgraded an app from vb6 to vb.net I've run into an issue where it takes about 15-20 times longer to write 1000 records to an MS Access database. (I'm sure it takes that...
3
by: Rabbit | last post by:
Dear All, I am development my web application on my PC, while its testing under the local area network environment (http://192.168.0.2/webapp/default.aspx), the speed and response are all very...
50
by: diffuser78 | last post by:
I have just started to learn python. Some said that its slow. Can somebody pin point the issue. Thans
3
by: Michael | last post by:
I work with a highly programmed Access database (some 15,000 lines of VBA code, much of it automating data entry on forms -- and believe me, it's very tight code). In Access 97, 2000, 2002, and...
8
by: SaltyBoat | last post by:
Needing to import and parse data from a large PDF file into an Access 2002 table: I start by converted the PDF file to a html file. Then I read this html text file, line by line, into a table...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.