Jump to content

How to optimize a php script


freelance84

Recommended Posts

Ok, i have a 2700 line php script which i am now trying to optimise, not too sure where to start.

 

With the max number of operations and max amount of html output the script takes 0.04 to 0.05seconds to complete.

 

I understand that asking for an average runtime of a script of this length is completely dependant on the balance between ram usage and hdd access. I'm completely new to optimization and from what i gather on the google searches it is more or less a case of trial and error? Calculating time differences as the script works down?

 

Any pointers here would be great.

 

Thanks in advance, John.

Link to comment
Share on other sites

Optimization comes from how well your code is designed/written and what techniques/methods you used.  Things like using string database performance, repeating code, images, even using regex operations when you don't have to.  Tools such as firebug and YSlow can also help you determine what is slowing down your pages.  But .04 and .05 don't seem like numbers to be worried about.

Link to comment
Share on other sites

KingPhilip:

Funny you post that link... thats where i started actually from the old google searches. Quite in depth. I was just wondering if there might be a few main pot holes to watch out for.

From what i gather it seems optimizing a script is a real balancing act between RAM and HDD access, unsetting variables when no longer needed...etc.

 

tibberous:

I'm just trying to cross all t's and dot all i's, the more you know the better you understand.

Link to comment
Share on other sites

For optimizing the script for server performance, I usually start with profiling the script with XDebug or Zend and dropping that into KCachegrind (Callgrind on Windows).  I have a blog post that goes into a bit more detail in the link in my sig.

 

Rasmus Lerdorf also does a ton of talks about PHP performance,  and you can get the slides for them at http://talks.php.net/index.php/PHP

Link to comment
Share on other sites

If your really interested in optimization, you might want to focus on the mysql end of it. Trying to improve 'general performance' is a waste of time. There are times where a huge database can cause you issues though, few things you might want to look at are:

 

- MySQL database indexes

- Proper hierarchical data storage (which is hard as fuck) http://dev.mysql.com/tech-resources/articles/hierarchical-data.html

- Pre-caching / lookup table creation with and without cron scripts

 

The key to good optimization is to only optimize the stuff that is slow enough to be an issue. Did you read xylex's sig? "The greatest inefficiencies come from solving problems you will never have.  -Rasmus"

 

PHP is fast, performance is only an issue in rare situations, and as servers become faster, optimization is only going to become less and less important.

Link to comment
Share on other sites

Cool, cheers for the tips.

 

"The greatest inefficiencies come from solving problems you will never have.  -Rasmus"

Couldn't agree more, but at the same time "ignorance is bliss" so just trying to get a better understanding of everything

 

 

It would be interesting to see what techniques, tools etc. you have used to increase performance for your script.  Keep us updated!

Link to comment
Share on other sites

Theres heaps you can do, for example if you just want to optimize the php code and not the site speed, search for "php code speed" . Youll find stuff like

 

Slower:

if($num !=''){
echo 'ok';
}

 

Faster:

if($num){
echo 'ok';
}

 

Slower:

print('ok');

 

Faster:

echo 'ok';

 

etc..

Link to comment
Share on other sites

Theres heaps you can do, for example if you just want to optimize the php code and not the site speed, search for "php code speed" . Youll find stuff like

 

Slower:

if($num !=''){
echo 'ok';
}

 

Faster:

if($num){
echo 'ok';
}

 

Slower:

print('ok');

 

Faster:

echo 'ok';

 

etc..

 

These kinds of micro optimization are a waste of one massive resource, time.

Link to comment
Share on other sites

For optimizing the script for server performance, I usually start with profiling the script with XDebug or Zend and dropping that into KCachegrind (Callgrind on Windows).  I have a blog post that goes into a bit more detail in the link in my sig.

 

Rasmus Lerdorf also does a ton of talks about PHP performance,  and you can get the slides for them at http://talks.php.net/index.php/PHP

 

Not to mention this tutorial I wrote:  http://www.flingbits.com/tutorial/view/xdebug-for-developing-debugging-and-profiling-php

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.