IT Community - Software Programming, Web Development and Technical Support

PHP Optimization Tips

This is a discussion on PHP Optimization Tips within the PHP Programming forums, part of the Web Development category; Hi, Your PHP scripts are recompiled every time unless the scripts are cached. Install a PHP caching product to typically ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #121 (permalink)  
Old 04-24-2008, 02:31 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: PHP Optimization Tips

Hi,

Your PHP scripts are recompiled every time unless the scripts are cached. Install a PHP caching product to typically increase performance by 25-100% by removing compile times.
__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #122 (permalink)  
Old 04-24-2008, 02:32 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: PHP Optimization Tips

Hi,

When echoing strings it's faster to separate them by comma instead of dot. Note: This only works with echo, which is a function that can take several strings as arguments.
__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #123 (permalink)  
Old 04-24-2008, 02:33 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: PHP Optimization Tips

Hi,

Surrounding your string by ' instead of " will make things interpret a little faster since php looks for variables inside "..." but not inside '...'. Of course you can only do this when you don't need to have variables in the string.
__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #124 (permalink)  
Old 04-24-2008, 02:34 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: PHP Optimization Tips

Hi,

A function call with one parameter and an empty function body takes about the same time as doing 7-8 $localvar++ operations. A similar method call is of course about 15 $localvar++ operations.
__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #125 (permalink)  
Old 04-24-2008, 02:40 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: PHP Optimization Tips

Hi,
  • It's better to use select statements than multi if, else if, statements.
  • Error suppression with @ is very slow.
  • Turn on apache's mod_deflate
__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #126 (permalink)  
Old 04-24-2008, 02:41 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: PHP Optimization Tips

Hi,
  • Do not use functions inside of for loop, such as for ($x=0; $x < count($array); $x) The count() function gets called each time.
  • Incrementing a local variable in a method is the fastest. Nearly the same as calling a local variable in a function.
__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #127 (permalink)  
Old 04-25-2008, 05:39 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: PHP Optimization Tips

Hi,

Just declaring a global variable without using it in a function also slows things down (by about the same amount as incrementing a local var). PHP probably does a check to see if the global exists.
__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #128 (permalink)  
Old 04-25-2008, 05:40 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: PHP Optimization Tips

Hi,

Method invocation appears to be independent of the number of methods defined in the class because I added 10 more methods to the test class (before and after the test method) with no change in performance.
__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #129 (permalink)  
Old 04-25-2008, 05:40 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: PHP Optimization Tips

Hi,

  • Do not implement every data structure as a class, arrays are useful, too
  • Don't split methods too much, think, which code you will really re-use
  • You can always split the code of a method later, when needed
__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #130 (permalink)  
Old 04-30-2008, 03:25 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: PHP Optimization Tips

Hi,

Achieving High Performance

When we talk about good performance, we are not talking about how fast your PHP scripts will run. Performance is a set of trade offs between scalability and speed. Scripts tuned to use fewer resources might be slower than scripts that perform caching, but more copies of the same script can be run at one time on a web server.
__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #131 (permalink)  
Old 04-30-2008, 03:26 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: PHP Optimization Tips

Hi,

Achieving High Performance

In the example below, A.php is a sprinter that can run fast, and B.php is a marathon runner than can jog forever at the nearly the same speed. For light loads, A.php is substantially faster, but as the web traffic increases, the performance of B.php only drops a little bit while A.php just runs out of steam.

__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #132 (permalink)  
Old 04-30-2008, 03:29 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: PHP Optimization Tips

Hi,

Achieving High Performance

Let us take a more realistic example to clarify matters further. Suppose we need to write a PHP script that reads a 250K file and generates a HTML summary of the file. We write 2 scripts that do the same thing: hare.php that reads the whole file into memory at once and processes it in one pass, and tortoise.php that reads the file, one line at time, never keeping more than the longest line in memory. Tortoise.php will be slower as multiple reads are issued, requiring more system calls.
__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #133 (permalink)  
Old 04-30-2008, 03:31 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: PHP Optimization Tips

Hi,

Achieving High Performance

Hare.php requires 0.04 seconds of CPU and 10 Mb RAM and tortoise.php requires 0.06 seconds of CPU and 5 Mb RAM. The server has 100 Mb free actual RAM and its CPU is 99% idle. Assume no memory fragmentation occurs to simplify things.

At 10 concurrent scripts running, hare.php will run out of memory (10 x 10 = 100). At that point, tortoise.php will still have 50 Mb of free memory. The 11th concurrent script to run will bring hare.php to its knees as it starts using virtual memory, slowing it down to maybe half its original speed; each invocation of hare.php now takes 0.08 seconds of CPU time. Meanwhile, tortoise.php will be still be running at its normal 0.06 seconds CPU time.
__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #134 (permalink)  
Old 04-30-2008, 03:43 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: PHP Optimization Tips

Hi,

In the table below, the faster php script for different loads is in bold:

HTML Code:
Connections  
	CPU seconds required to satisfy 1 HTTP request
	CPU seconds required to satisfy 10 HTTP requests
	CPU seconds required to satisfy 11 HTTP requests

hare.php
	0.04
	0.40
	0.88
(runs out of RAM)

tortoise.php
	0.06
	0.60
	0.66

As the above example shows, obtaining good performance is not merely writing fast PHP scripts. High performance PHP requires a good understanding of the underlying hardware, the operating system and supporting software such as the web server and database.
Attached Files
File Type: doc Achieving High Performance.doc (26.5 KB, 2 views)
__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #135 (permalink)  
Old 05-02-2008, 06:27 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: PHP Optimization Tips

Hi,

Optimize table:
This is useful when the table contains overheads. After massive deletions of rows or length changes for VARCHAR fields, lost bytes remain in the table. PhpMyAdmin warns us in various places (for example, in the Structure view) if it feels the table should be optimized. This operation is a kind of defragmentation for the table. In MySQL 4.x, this operation works only on tables in the MyISAM, Berkeley (BDB), and InnoDB engines. In MySQL 5.x, it works only on tables in the MyISAM, InnoDB, andARCHIVE engines.
__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #136 (permalink)  
Old 05-07-2008, 01:39 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: PHP Optimization Tips

Hi,
  • Make use of the countless predefined functions
  • If you have very time consuming functions in your code, consider writing them as C extensions
  • You can always split the code of a method later, when needed
__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.discussweb.com/php-programming/4357-php-optimization-tips.html
Posted By For Type Date
The Storyteller This thread Refback 11-30-2007 07:09 AM
del.icio.us/subscriptions/KoerPoiss This thread Refback 11-30-2007 06:47 AM
Planet PHP Japan This thread Refback 11-30-2007 04:35 AM

Similar Threads
Thread Thread Starter Forum Replies Last Post
All You Need for Search Engine Optimization (SEO) vozduhh Search Engine Optimization 1 02-20-2008 11:40 AM
SEO optimization question.? navjeet Search Engine Optimization 1 09-04-2007 11:54 AM
Image Alt Tag Optimization Tips vadivelanvaidyanathan Search Engine Optimization 0 03-30-2007 03:02 AM
Title Tag Optimization Tips vadivelanvaidyanathan Search Engine Optimization 0 03-30-2007 02:58 AM
SEO Optimization - Page Title drecko Search Engine Optimization 2 02-19-2007 01:28 AM


All times are GMT -7. The time now is 04:29 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0