Page 1 of 2

What makes a PHP Script run faster?

Posted: Wed Jul 13, 2005 3:52 pm
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

Posted: Wed Jul 13, 2005 3:57 pm
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?

Posted: Wed Jul 13, 2005 4:03 pm
by Burrito
yes, wrong something is...check your code before spending any $$ you should.

Posted: Wed Jul 13, 2005 4:08 pm
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

Posted: Wed Jul 13, 2005 4:15 pm
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).

Posted: Wed Jul 13, 2005 4:29 pm
by onion2k
It's generating HTML? Surely that'd be all selects then?

Something is seriously wrong with that code.

Re: What makes a PHP Script run faster?

Posted: Wed Jul 13, 2005 4:30 pm
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?

Posted: Wed Jul 13, 2005 4:58 pm
by timvw
time do some profiling...

Posted: Wed Jul 13, 2005 5:35 pm
by patrikG
timvw wrote:time do some profiling...
and to start optimising those queries.

Posted: Wed Jul 13, 2005 6:15 pm
by McGruff
I'd recommend Xdebug for profiling, and apache ab.

Posted: Wed Jul 13, 2005 8:26 pm
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 :)

Posted: Thu Jul 14, 2005 9:51 am
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.

Posted: Thu Jul 14, 2005 10:41 am
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?

Posted: Thu Jul 14, 2005 10:45 am
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?

Posted: Thu Jul 14, 2005 10:52 am
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