freelance84 Posted January 14, 2011 Share Posted January 14, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/224465-how-to-optimize-a-php-script/ Share on other sites More sharing options...
Maq Posted January 14, 2011 Share Posted January 14, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/224465-how-to-optimize-a-php-script/#findComment-1159544 Share on other sites More sharing options...
Maq Posted January 14, 2011 Share Posted January 14, 2011 (Moving to Miscellaneous unless you have a specific PHP Coding question) Quote Link to comment https://forums.phpfreaks.com/topic/224465-how-to-optimize-a-php-script/#findComment-1159547 Share on other sites More sharing options...
freelance84 Posted January 14, 2011 Author Share Posted January 14, 2011 firebug and YSlow Thanks for the tip. Also, apologies for the incorrect thread placement. Quote Link to comment https://forums.phpfreaks.com/topic/224465-how-to-optimize-a-php-script/#findComment-1159554 Share on other sites More sharing options...
Philip Posted January 14, 2011 Share Posted January 14, 2011 There are a couple of pretty good reminders/tips in here: http://phplens.com/lens/php-book/optimizing-debugging-php.php As Maq said though, it's really dependent on your code. Quote Link to comment https://forums.phpfreaks.com/topic/224465-how-to-optimize-a-php-script/#findComment-1159568 Share on other sites More sharing options...
tibberous Posted January 15, 2011 Share Posted January 15, 2011 What does the script do that it's 2700 lines long? I can count on 1 hand the times I've actually needed to optimize something in PHP - and normally just by adding indexs to tables. Who cares that it takes .05 seconds? If it works, be happy. Quote Link to comment https://forums.phpfreaks.com/topic/224465-how-to-optimize-a-php-script/#findComment-1159677 Share on other sites More sharing options...
freelance84 Posted January 15, 2011 Author Share Posted January 15, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/224465-how-to-optimize-a-php-script/#findComment-1159763 Share on other sites More sharing options...
xylex Posted January 15, 2011 Share Posted January 15, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/224465-how-to-optimize-a-php-script/#findComment-1159818 Share on other sites More sharing options...
tibberous Posted January 16, 2011 Share Posted January 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/224465-how-to-optimize-a-php-script/#findComment-1160159 Share on other sites More sharing options...
freelance84 Posted January 17, 2011 Author Share Posted January 17, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/224465-how-to-optimize-a-php-script/#findComment-1160760 Share on other sites More sharing options...
Maq Posted January 17, 2011 Share Posted January 17, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/224465-how-to-optimize-a-php-script/#findComment-1160777 Share on other sites More sharing options...
dreamwest Posted January 18, 2011 Share Posted January 18, 2011 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.. Quote Link to comment https://forums.phpfreaks.com/topic/224465-how-to-optimize-a-php-script/#findComment-1161263 Share on other sites More sharing options...
trq Posted January 18, 2011 Share Posted January 18, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/224465-how-to-optimize-a-php-script/#findComment-1161268 Share on other sites More sharing options...
dreamwest Posted January 18, 2011 Share Posted January 18, 2011 It always good to keep those in mind when doing a rewrite. Quote Link to comment https://forums.phpfreaks.com/topic/224465-how-to-optimize-a-php-script/#findComment-1161283 Share on other sites More sharing options...
freelance84 Posted January 20, 2011 Author Share Posted January 20, 2011 Keep us updated! Whilst organising.... if($action == 1) { include_once 'bigLooping.php'; } The big loop was 500lines long and originally just sat in amongst the 2700. Now is only included when needed. This has shaved some time off Quote Link to comment https://forums.phpfreaks.com/topic/224465-how-to-optimize-a-php-script/#findComment-1162525 Share on other sites More sharing options...
gizmola Posted January 20, 2011 Share Posted January 20, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/224465-how-to-optimize-a-php-script/#findComment-1162708 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.