What makes a PHP Script run faster?

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Deemo
Forum Contributor
Posts: 418
Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC

What makes a PHP Script run faster?

Post by Deemo »

Right now i have a program that processes ALOT of data. On my 3Ghz processor with 1 Gb memory it takes over 6 seconds to process aproximately 40 rows of data.

Now, my question is, which has the most impact on running lots of queries with, more memory or more processing power?

Dell is having a special on their servers right now, enabling me to get much cheaper dual core processors. Do you think i should get a single core processor with more Ghz and Memory? or get a dual core which is more expensive?

price is not as huge of an issue, but i need this to run as fast as possible
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

40 rows at aprox 6 seconds...? I've ran over 70,000 queries in less time then that. What does your query look like?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

yes, wrong something is...check your code before spending any $$ you should.
Deemo
Forum Contributor
Posts: 418
Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC

Post by Deemo »

its not just one query its a good number per row. your right though, it doesnt seem right.

Most of the code is in a loop, and this loop is the one that happens 40 times. the final output is 2150 lines of HTML

does that seem a bit normal? to put it into perspective, there are lots of drop down menus
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

It should still be able to process faster than that if it's a dedicated server. Why not try dumping out microtime() in some places to see what's taking the longest.

If there's no single place that's taking longer than it should, maybe your code can be optimized a bit (ie: not generating 100% of the html each loop - use variables for common pieces of code).
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

It's generating HTML? Surely that'd be all selects then?

Something is seriously wrong with that code.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: What makes a PHP Script run faster?

Post by onion2k »

Deemo wrote:On my 3Ghz processor with 1 Gb memory it takes over 6 seconds to process aproximately 40 rows of data.
Having a gig of RAM is entirey irrelevent if PHP is limited to using a maximum of 8 meg. Have you increased it from the default?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

time do some profiling...
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

timvw wrote:time do some profiling...
and to start optimising those queries.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

I'd recommend Xdebug for profiling, and apache ab.
Deemo
Forum Contributor
Posts: 418
Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC

Post by Deemo »

i tried setting the memory limit higher but it didnt do any good.

the queries are simple queries. Nothing more advanced that a SELECT * FROM Table WHERE ID=1 or an update query. its not even that theres alot of queries, its just that there is so much to process. I would host it up somewhere but i dont have any host to run it off of.

Let me try and explain how my code works and you guys can try and help from there. It starts by getting a simple query, selecting all the tasks from the database. thats stored in a 2 dimentional array of rows an columns. i then start by displaying the due date of the task. thats 3 drop down menus, with number of days (1-31) months (1-12) year (2005-2010). The thing is, this isnt just a normal drop down menu, this is a hidden menu, so when you put it in focus, thats when it appears (see my previous post here). Then it goes on display the client name, project name, and some other miscelaneos data. Finally it starts a new row, but this row is made up of 4 drop down menus, each with 6 items each. Next to each drop down is a text box with the number of hours.

Here is an image of what it looks like Image


I noticed that it takes longest during the row with the 4 drop down menus. Why do you think this is?


edit: i was looking at xdebug and tryed to install it but i completely lost myself and i dont know what im doing anymore

edit 2: i am 100% sure that the 4 drop down menus are bottlenecking it. When i comment it out, it takes .4 seconds to load the page. ill have to find a way to optimize this now


edit3:
im going to post the code for the problem area. Hopefully someone will notice whats wrong and help me fix it. ask any question about the code, ill try and comment it as best i can

Code: Select all

for ($i = 1; $i <= $cfg['numMaxAdvisors']; $i++)     //$cfg['numMaxAdvisors'] is set to 4
{
  echo '<select name="advisor'.$i.'[]" onBlur="changed('.$key.')">';
  
  foreach ($blankadvisorlist as $tempkey =>$tempvalue)        //$blankadvisorlist is an array (see below)
  {
    echo '<option value="'.$tempvalue ->mID.'"';
    if ($task ->mAdvisors[$i - 1] ->mID == $tempvalue ->mID)    //Select the correct advisor
      echo " selected";
    echo '>'.$tempvalue ->mName.'</option>';      //Display advisor name
  }
  echo '</select><input type="text" name="hours'.$i.'[]" size=2 value="'.$task ->mNumHours[$i - 1].'"  onBlur="changed('.$key.')" > ';     //Add the text box next to selection menu
  
}

Code: Select all

$blankadvisorlist Array(
&#1111;0] =&gt; &quote;&quote;
&#1111;1] =&gt; &quote;Name1&quote;
&#1111;2] =&gt; &quote;Name2&quote;
...
&#1111;6] =&gt; &quote;Name6&quote;
)
thanks alot for any help you can provide and for actually sitting down and helping me through this :)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

I can't think of anything concrete that would make this faster. It seems though, that you should be able to optimize the foreach loop a bit. It is just looping through a static array and, for the most part, outputing identical code each iteration.

Also, change

Code: Select all

echo " selected";
to

Code: Select all

echo ' selected';
It probably won't give you a noticable difference, but it's better than nothing.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Deemo
Forum Contributor
Posts: 418
Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC

Post by Deemo »

im a bit confused about how i could optimize the foreach loop. in each option of the drop down i have to check and see if its the one thats supposed to be selected. is there any other way to do it thats more efficient?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Ha - I'm not sure either. I was really just brainstorming out loud. Could str_replace be used after the pulldown is generated?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

pickle wrote: Also, change

Code: Select all

echo " selected";
to

Code: Select all

echo ' selected';
even better, change it to:

Code: Select all

echo ' selected="selected"';
to conform to new standards....
8O
Post Reply