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, Set the maxvalue for your for-loops before and not in the loop. Code: $maxvalue = 100/10; for($i=...


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

Register FAQ Members List Calendar Mark Forums Read
  #41 (permalink)  
Old 11-07-2007, 02:07 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default Re: PHP Optimization Tips

Hi,

Set the maxvalue for your for-loops before and not in the loop.
Code:
$maxvalue = 100/10;
for($i=0; $i<$maxvalue; $i++){
   // Some code
}
is faster than:
Code:
for($i=0; $i<100/10; $i++){
   // Some code
}
because the value is calculated once instead of ten times.

Regards,
R.Kamalakannan.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #42 (permalink)  
Old 11-07-2007, 02:26 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default Re: PHP Optimization Tips

Hi,

Many code blocks might slow down the interpretation a little bit.
Code:
<?
   ...
   ...
   ...
?>
is faster than
Code:
<? ... ?>
<? ... ?>
<? ... ?>
Regards,
R.Kamalakannan.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #43 (permalink)  
Old 11-07-2007, 04:47 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: PHP Optimization Tips

if you want to assign variable to one of the array indexes, don’t do $var=$arr[‘foo’], better choice would be use references. $var=&$arr[‘foo’], now you just creating a reference to $arr[‘foo’] instead of duplicating it’s value.


use $_SERVER[’REQUEST_TIME’] instead of every time calling time() function. For example declare constant at the config file: define(“TIME”, $_SERVER[’REQUEST_TIME’]) and use TIME when you want to know timestamp.
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #44 (permalink)  
Old 11-07-2007, 04:48 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: PHP Optimization Tips

ctype functions are faster than regular expressions. More info at PHP: Character Type Functions - Manual
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #45 (permalink)  
Old 11-07-2007, 05:24 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: PHP Optimization Tips

- $arr[‘id’] works much more faster than $arr[id]
- if you need to print a bigger piece of text without PHP variables just exit PHP (?>) and enter it when you need it again (<?php).
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #46 (permalink)  
Old 11-08-2007, 03:54 AM
Gopisoft Gopisoft is offline
D-Web Sr.Programmer
 
Join Date: Feb 2007
Posts: 117
Gopisoft is on a distinguished road
Default Re: PHP Optimization Tips

Hi,

Compiler/Opcode Caches


  • This cycle happens for every include file, not just for the "main" script.
  • Compilation can easily consume more time than execution.
  • Each PHP script is compiled only once for each revision.
  • Reduced File IO, opcodes are being read from memory instead of being parsed from disk.
  • Opcodes can optimised for faster execution.

Thanks,
R.Gopi.
Attached Images
File Type: jpg php_opcode.JPG (17.5 KB, 11 views)
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #47 (permalink)  
Old 11-08-2007, 04:13 AM
Gopisoft Gopisoft is offline
D-Web Sr.Programmer
 
Join Date: Feb 2007
Posts: 117
Gopisoft is on a distinguished road
Default Re: PHP Optimization Tips

Hi,

Optimize the Web Server File I/O Operations
  • Keep DirectoryIndex file list as short as possible.
  • Whenever possible disable .htaccess via AllowOverride none.
  • Use Options FollowSymLinks to simplify file access process in Apache.
  • If logs are unnecessary disable them.
  • If logging is a must, log everything to 1 file and break it up during the analysis stage.

Thanks,
R.Gopi.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #48 (permalink)  
Old 11-08-2007, 04:17 AM
Gopisoft Gopisoft is offline
D-Web Sr.Programmer
 
Join Date: Feb 2007
Posts: 117
Gopisoft is on a distinguished road
Default Re: PHP Optimization Tips

Hi,

Tuning PHP Configuration
  • register_globals = Off **
  • magic_quotes_gpc = Off
  • expose_php = Off
  • register_argc_argv = Off
  • always_populate_raw_post_data = Off **
  • session.use_trans_sid = Off **
  • session.auto_start = Off **
  • session.gc_divisor = 1000 or 10000
  • output_buffering = 4096

Thanks,
R.Gopi
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #49 (permalink)  
Old 11-08-2007, 04:27 AM
Gopisoft Gopisoft is offline
D-Web Sr.Programmer
 
Join Date: Feb 2007
Posts: 117
Gopisoft is on a distinguished road
Default Re: PHP Optimization Tips

Hi,

Tips of Tuning PHP Including / File Accessing

Whenever opening files or including scripts into the main script try to specify a full path or at least an easily resolvable partial path.

Inefficient Approach:
PHP Code:
<?php include "file.php"?>
Performance Friendly Approach:
PHP Code:
<?php
include "/path/to/file.php";
// or
include "./file.php";
?>
Thanks,
R.Gopi.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #50 (permalink)  
Old 11-08-2007, 04:47 AM
Gopisoft Gopisoft is offline
D-Web Sr.Programmer
 
Join Date: Feb 2007
Posts: 117
Gopisoft is on a distinguished road
Default Re: PHP Optimization Tips

Hi,
Optimizing the str_replace()
The str_replace() function in PHP can be slow, due it’s duplication of data even if no replacement is being performed.

PHP Code:
$src_str file_get_contents("CONTENT_FILE_PATH");
$src = array('abc'123'text');
$dst = array('cba'321'txet');
// eliminate unnecessary replacement attempts
foreach ($src as $k => $v)
if (
strpos($src_str$src) === FALSE)
unset(
$src[$k], $dst[$k]);
if (
$src$new_str str_replace($src$dst$src_str); 
Click to view the difference of strtr() and str_replace()

Thanks,
R.Gopi
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #51 (permalink)  
Old 11-08-2007, 09:35 PM
Gopisoft Gopisoft is offline
D-Web Sr.Programmer
 
Join Date: Feb 2007
Posts: 117
Gopisoft is on a distinguished road
Default Re: PHP Optimization Tips

HI,

Web Server: Syscalls

Syscall is function executed by the Kernel. The goal is to minimise the number of these calls needed to perform a request.
  • Do not enable ExtendedStatus.
  • For Deny/Allow rules use IPs rather then domains.
  • Do not enable HostnameLookups.
  • Keep ServerSignature off

Thanks,
R.Gopi.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #52 (permalink)  
Old 11-09-2007, 10:40 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: PHP Optimization Tips

Some more useful tips
* printf is much more slower than echo or print, it must handle additional parameters. But echo is faster than print, despite of that both are constructors, print is returning boolean result, echo returns nothing.
* when searching string in another string strstr() is faster than preg functions, however if you want to know if that string exists use strpos().
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #53 (permalink)  
Old 11-12-2007, 04:09 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: PHP Optimization Tips

How many ways your web server can utilize PHP to generate web pages?

Mainly there are three ways
***************************

The first method is to use PHP as a CGI "wrapper". When run this way, an instance of the PHP interpreter is created and destroyed for every page request (for a PHP page) to your web server. Because it is destroyed after every request, any resources that it acquires (such as a link to an SQL database server) are closed when it is destroyed. In this case, you do not gain anything from trying to use persistent connections -- they simply don't persist.

The second, and most popular, method is to run PHP as a module in a multiprocess web server, which currently only includes Apache. A multiprocess server typically has one process (the parent) which coordinates a set of processes (its children) who actually do the work of serving up web pages. When a request comes in from a client, it is handed off to one of the children that is not already serving another client. This means that when the same client makes a second request to the server, it may be served by a different child process than the first time. When opening a persistent connection, every following page requesting SQL services can reuse the same established connection to the SQL server.

The last method is to use PHP as a plug-in for a multithreaded web server. Currently PHP 4 has support for ISAPI, WSAPI, and NSAPI (on Windows), which all allow PHP to be used as a plug-in on multithreaded servers like Netscape FastTrack (iPlanet), Microsoft's Internet Information Server (IIS), and O'Reilly's WebSite Pro. The behavior is essentially the same as for the multiprocess model described before. Note that SAPI support is not available in PHP 3.
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #54 (permalink)  
Old 11-14-2007, 10:26 PM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: PHP Optimization Tips

Hi Gopi,

your posts are very very useful for us, so please keep on postions these kind of valuable posts.
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #55 (permalink)  
Old 11-14-2007, 10:50 PM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: PHP Optimization Tips

PHP is a very fast programming language, but there is more to optimizing PHP than just speed of code execution.

here, we explain why optimizing PHP involves many factors which are not code related, and why tuning PHP requires an understanding of how PHP performs in relation to all the other subsystems on your server, and then identifying bottlenecks caused by these subsystems and fixing them. We also cover how to tune and optimize your PHP scripts so they run even faster.
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #56 (permalink)  
Old 11-14-2007, 10:52 PM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: PHP Optimization Tips

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 tradeoffs 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.

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.



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.

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.
Attached Images
File Type: jpg Image1.jpg (12.8 KB, 0 views)
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #57 (permalink)  
Old 11-14-2007, 10:57 PM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: PHP Optimization Tips

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

----------------------------------------------------------------------------------------------
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.
__________________
Thanks & Regards
Sabari...

Last edited by Sabari : 11-14-2007 at 11:01 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #58 (permalink)  
Old 11-14-2007, 11:02 PM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: PHP Optimization Tips

Bottlenecks
The hare and tortoise example has shown us that bottlenecks cause slowdowns. With infinite RAM, hare.php will always be faster than tortoise.php. Unfortunately, the above model is a bit simplistic and there are many other bottlenecks to performance apart from RAM:
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #59 (permalink)  
Old 11-14-2007, 11:02 PM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: PHP Optimization Tips

(a) Networking
Your network is probably the biggest bottleneck. Let us say you have a 10 Mbit link to the Internet, over which you can pump 1 megabyte of data per second. If each web page is 30k, a mere 33 web pages per second will saturate the line.

More subtle networking bottlenecks include frequent access to slow network services such as DNS, or allocating insufficient memory for networking software.
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #60 (permalink)  
Old 11-14-2007, 11:03 PM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: PHP Optimization Tips

(b) CPU
If you monitor your CPU load, sending plain HTML pages over a network will not tax your CPU at all because as we mentioned earlier, the bottleneck will be the network. However for the complex dynamic web pages that PHP generates, your CPU speed will normally become the limiting factor. Having a server with multiple processors or having a server farm can alleviate this.
__________________
Thanks & Regards
Sabari...
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